ajax - Why does AngularJS $http .success promise work but .then doesn't? -


i've been using following code doing ajax calls in angular worked fine far:

  function getdata (url) {     var deferred = $q.defer();     $http.get(url, {       cache: true     }).success(function (data) {       deferred.resolve(data); // works     });     return deferred.promise;   } 

as can see there success handler. when wanted add error handler, noticed docs state success , error depracated , should not used more (https://docs.angularjs.org/api/ng/service/$http). instead .then promise should used, changed code this:

  function getdata (url) {     var deferred = $q.defer();     $http.get(url, {       cache: true     }).then(function (data) {       deferred.resolve(data); // not called :(     }, function () {       deferred.resolve(false); // not called :(     });     return deferred.promise;   } 

now stopped working. .then never called. why? i'm on latest angular version 1.4

ok, found problem. actually, there 2 problems.

first, had redundant promise removed:

  function getdata (url) {     return $http.get(url, {cache: true});   } 

second, returned data .then seems little different .success. instead of returning plain result data, returns object meta data call. so, in order data, i'd have access data.data:

  pub.getlist = function () {     return getdata(serviceurl + "?c=latest").then(function (result) {       var list = result.data;       precachearticles(list);       return list;     });   }; 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -