简体   繁体   中英

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);

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM