Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs - $http promise error handling

I'm having a problem. I'm trying to make a $http call with the .then function insted .sucssess and .error. But although i change the url of the request to a non existing one, it is always executing the succes handler:

$http.get('data/myjson.json').then(onSuccess, onError);

onSuccess(data){};
onError(data){};

Also, the data parameter has a status of 404, indicating the failure, but the onError got never execute.

some body can explain how this really work?

thanks!

like image 533
user2887964 Avatar asked Oct 16 '13 20:10

user2887964


People also ask

Which is correct syntax for making a get call and handling the error?

get(url) . then(function (response) { console. log('get',response) }) . catch(function (data) { // Handle error here });

What is then() in angular?

The then() method takes two callback functions as parameters and is invoked when a promise is either resolved or rejected. The catch() method takes one callback function and is invoked when an error occurs.

What is $location in AngularJS?

The $location in AngularJS basically uses a window. location service. The $location is used to read or change the URL in the browser and it is used to reflect that URL on our page. Any change made in the URL is stored in the $location service in AngularJS.

Which is not recommended in AngularJS?

It is tempting to do too much work in the AngularJS controller. After all, the controller is where the view first has access to JavaScript via $scope functions. However, doing this will cause you to miss out on code sharing across the site and is not recommended by AngularJS documentation.


1 Answers

This code works for me:

$http.get('data/myjson.json').then(onSuccess, onError);

function onSuccess(data) {
}
function onError(data) {
}
like image 145
Manuel van Rijn Avatar answered Nov 04 '22 21:11

Manuel van Rijn