Difference between angular_JS $ http.get and callback

I would like to understand the difference between a callback and a successful callback when called via http get. When I use the callback then it returns the data, but with a successful callback it does not. Below is the code

Then callback

$http.get(url). then(function(response) { response.data.data;}); 

Success callback

 $http.get(url). success(function(response) { response.data;}); 
+6
source share
3 answers

Your problem seems to be around this:

$ http.get ('/ someUrl'). success (function ( data , status, headers, config) {

this is another return from then ,

then method for registering callbacks, and these callbacks will receive one argument - the object representing the response

In other words, you should do this:

 $http.get(...).success(function(data){ console.log(data) }) $http.get(...).then(function(response){ console.log(response.data) }) 

And of course, the differences are in the chain, but don't seem to be related to your problem:

then()

If the chain is then() , callbacks will be executed sequentially after each of them completes, since it returns a new promise object in each chain

success() (deprecated * along with error() )

If you bind success() calls, the callbacks will be executed in parallel, because it returns the original promise object

* success and error are deprecated, see $ http docs withdrawal notification section

+6
source

you can use either of .then or .success, and the callback code depends on which method you use.

.then () will have two arguments at the beginning - it is a success handler, and the second - an error handler. The success handler inside then () can be written in some other way only with .success.the main difference between the successful master inside then () and .success .success function will have 4 arguments (data, status, headers, config), where as a success handler in then () there will be only one argument that has (data, status, headers, config) nested in this one nothinf argument but you can access it as (response.data, response.status, etc.) d.)

0
source

Important recommendation of angularjs.org :

The $ http legacy utilities promise success and error are out of date . Use the standard method instead. If the $httpProvider.useLegacyPromiseExtensions parameter is set to false, then these methods will $httpProvider.useLegacyPromiseExtensions a $ http / legacy error.

0
source

Source: https://habr.com/ru/post/981146/


All Articles