Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS $http ajax request is not asynchronous and causes page to hang

I have a service where I am pulling data from server. When I click the button to send out the request to server through this service, the window freezes until I receive a response from server. Is there anything I can do to make this request asynchronous ?

Here is my service.

app.factory('service', function($http) {
  return {
    getLogData : function(startTime,endTime){
      return $http({
        url:     baseURL + 'getLogData',
        method:  'GET',
        async:   true,
        cache:   false,
        headers: {'Accept': 'application/json', 'Pragma': 'no-cache'},
        params:  {'startTime': startTime , 'endTime': endTime}
      });
    }
  };
)};

HTML.

<button ng-click="getData()">Refresh</button>
<img src="pending.gif" ng-show="dataPending" />

Code

$scope.getData = function(){
  service.getLogData().success(function(data){
    //process data
  }).error(function(e){
    //show error message
  });
}
like image 956
lostpacket Avatar asked Sep 27 '13 16:09

lostpacket


3 Answers

While there is some argument about the pros and cons of your approach, I am thinking that the problem is answered here: AJAX call freezes browser for a bit while it gets response and executes success

To test if this in fact part of the problem, dummy up a response and serve it statically. I use Fiddler or WireShark to get the response and then save to a file like testService.json. XHR and all of it's various derivatives like $HTTP $.ajax see it as a service though the headers might be slightly different.

like image 162
Nathaniel Johnson Avatar answered Nov 05 '22 16:11

Nathaniel Johnson


Use the success promise, and wrap up the log data in a set of objects that you can attach to a $scope.

So instead of having your service have a blocking method, have it maintain a list of "LogEntries".

   // constructor function
   var LogEntry = function() {
      /*...*/
   }

   var logEntries = [];

   // Non-blocking fetch log data
   var getLogData = function() {
        return $http({
            url : baseURL + 'getLogData',
            method : 'GET',
            async : true,
            cache : false,
            headers : { 'Accept' : 'application/json' , 'Pragma':'no-cache'},
            params : {'startTime' : startTime , 'endTime' : endTime}
        }).success(function(data) {;
           // for each log entry in data, populate logEntries
           // push(new LogEntry( stuff from data ))...
        };
   } 

Then in your controller, inject your service and reference this service's log data array so Angular will watch it and change the view correctly

   $scope.logEntries = mySvc.logEntries;

Then in the HTML, simply do something over logEntries:

  <p ng-repeat="logEntry in logEntries">
    {{logEntry}}
  </p>
like image 22
Doug T. Avatar answered Nov 05 '22 15:11

Doug T.


use this code to config

$httpProvider.useApplyAsync(true);
like image 2
Behnam Mohammadi Avatar answered Nov 05 '22 14:11

Behnam Mohammadi