1

Is there a way to send an error back to JS when we do a $http.post?
Right now if there is an error server side, I return a response tagged as "error" but it's captured in success on the Angularjs side.
It would be better if I send directly to error instead of having a condition in the success of Angular.
Is that possible?

2
  • 2
    possible duplicate of AngularJS, $resource and PHP errors Commented Jun 23, 2013 at 20:20
  • You haven't mentioned what server side tech you're using, but with asp.net web api, you can send back http error codes instead of sending back a String. Commented Jun 24, 2013 at 1:03

1 Answer 1

4

According to the AngularJS documentation for $http:

A response status code between 200 and 299 is considered a success status and will result in the success callback being called. Note that if the response is a redirect, XMLHttpRequest will transparently follow it, meaning that the error callback will not be called for such responses.

If you want your HTTP call to result in the error handler being called you need to return a non 2XX response code (and also non-redirect as mentioned). You can find a pretty full list of HTTP response codes here

Sign up to request clarification or add additional context in comments.

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.