9

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!

2
  • What version of Angular are you using? Commented Oct 16, 2013 at 20:53
  • I'm using angular 1.0.7. Commented Oct 17, 2013 at 12:01

3 Answers 3

12

This code works for me:

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

function onSuccess(data) {
}
function onError(data) {
}
Sign up to request clarification or add additional context in comments.

5 Comments

Hi Manuel, thanks for your answer. The onError function, got execute when you use a wrong url for the request? In case of success, my code works, the problem is error handling
What do you mean with error handling? What response do you get to handle errors on?
I mean, when i try tu put a wrong url (for example dataaaa/myjson.com), i want the onError to be executed, but the onSucces is being executed instead
If i'm correct, this should work as $http.get('data/myjson.json').succes().error, should it?
@ManuelvanRijn Good work. It works for me.. (upvote)
2
  $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 ...

Comments

1

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) {
}    

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.