Shadoweb
Shadoweb

Reputation: 6306

how to send a post error in angularjs

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?

Upvotes: 1

Views: 271

Answers (1)

Michael McGuire
Michael McGuire

Reputation: 3900

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

Upvotes: 4

Related Questions