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?
-
2possible duplicate of AngularJS, $resource and PHP errorsStewie– Stewie2013-06-23 20:20:17 +00:00Commented 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.mnsr– mnsr2013-06-24 01:03:31 +00:00Commented Jun 24, 2013 at 1:03
Add a comment
|
1 Answer
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