1

I try to use angular-bootstrap's typeahead plugin with $http service.At first, I use $http.success() like this:

$scope.filterProvinces = function(val){
    return service.provinceList({
         pageSize : 10,
         queryKey : val
     }).success(function(data, status, headers, config){
          return data.data.list;
       }
     ).error(function(data, status, headers, config){
          alert("no result!");
          return ;
     });
}

but it didn't work, so I change the code , like this:

$scope.filterProvinces = function(val){
    return service.provinceList({
         pageSize : 10,
         queryKey : val
     }).then(function(response){
         return response.data.data.list;
     });
}

and it was working, so I was confused. Why then() is ok but success() is not right.

1

1 Answer 1

3

If you check the AngularJS source, you'll find that .success() actually takes a function itself as an argument, which is then invoked, passing in the deconstructed response object, ultimately calling .then() to resolve the promise.

Because of the lack of clarity, this was actually deprecated and the documentation for $http recommends the use of .then() instead.

The $http legacy promise methods success and error have been deprecated. Use the standard then method instead. If $httpProvider.useLegacyPromiseExtensions is set to false then these methods will throw $http/legacy error.

It's worth checking to make sure that this error is not being thrown.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.