Michael Yuxi Dong
Michael Yuxi Dong

Reputation: 181

Angularjs $http.get doesn't call the callback

I do the $http.get request as soon as my javascript loaded, it works fine in 90% percent of cases, however, sometimes it just dont call the success or error callback, I'm not sure if the request is actually sent or it just doesn't call the callback, it's weird because it works good in 90% of cases, and 100% if I run it on my own computer, if I put this onto remote server, this case will happen...

    $scope.load = function(){
    console.log("Sending request at "+Date());
    $http.get(requestUrl).
      success(function(data, status, headers, config) {
        console.log("Loading Question Succeed");
        $scope.Content = data.Html;
        data = data.Data;
      }).
      error(function(data, status, headers, config) {
        console.log("Loading Question Failed");
      });
};
$timeout($scope.load);

Upvotes: 1

Views: 2750

Answers (1)

mindparse
mindparse

Reputation: 7225

You may want to consider using a then() method rather than the traditional success\failure approach. Have a read up on promises or check out this post that explains the differences - Angular HttpPromise: difference between `success`/`error` methods and `then`'s arguments

In the post above take a look at one of the answers at the end which gives at example of using the then() method and also a catch()

Give it a try and see if it gives you more consistent results or if anything is ending up in the catch()

Upvotes: 1

Related Questions