The syntax for using .success
was accurate until Angular v1.4.3.
From versions leading up to Angular v1.6, you must utilize the then
method. This function takes in two arguments: a success
and an error
callback that will be triggered with a response object.
By employing the then()
method, link a callback
function to the generated promise
.
An example implementation would look like this:
app.controller('MainCtrl', function ($scope, $http){
$http({
method: 'GET',
url: 'api/url-api'
}).then(function (response){
},function (error){
});
}
For further information, please visit the provided link.
Shortcut
methods are also accessible.
$http.get('api/url-api').then(successCallback, errorCallback);
function successCallback(response){
//success code
}
function errorCallback(error){
//error code
}
The data within the response should be formatted as JSON
.
JSON serves as an effective means of transferring data, particularly when working with AngularJS.
The primary distinction lies in the fact that the .then()
invocation yields a promise
(resolved by a value returned from a callback
) whereas .success()
follows a more traditional approach of registering callbacks
without returning a promise
.