user2887964
user2887964

Reputation: 91

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!

Upvotes: 9

Views: 16600

Answers (3)

vineet
vineet

Reputation: 14236

Both way can be use for ng promise error handling:-

First:

$http.get('data/myjson.json')
.success( successHandler )
.error( errorHandler )

function successHandler(data) {
}
function errorHandler(data) {
}

Second:

$http.get('data/myjson.json').then(successHandler, errorHandler);

function successHandler(data) {
}
function errorHandler(data) {
}    

Upvotes: 1

Sunith Saga
Sunith Saga

Reputation: 609

  $http.get(url).then(onSuccess, onerror);   
   function onSuccess(response) {
    //Success message here...
                                    }
    function onerror(data) {

                if(data.status==404) {
                    alert('Invalid URl')
                            return;
                }

    }

Just try this one ...

Upvotes: 2

Manuel van Rijn
Manuel van Rijn

Reputation: 10305

This code works for me:

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

function onSuccess(data) {
}
function onError(data) {
}

Upvotes: 12

Related Questions