JQuery AJAX calls error callback on 202 response-shouldnt, which is a successful callback?

Well its all in the title. I try to verify the user password before performing destructive actions:

$.ajax({ type: 'POST', url: '/path/to/post.json', data: { password: '**********' }, success: function() { console.log("Success!"); }, error: function() { console.log("Error!"); } }); 

In the console:

 202 Accepted 123ms Error! 

I thought that 403 Forbidden for the wrong password and 202 Accepted for the correct password, there will be suitable response codes, but I know little about HTTP to be honest.

jQuery version 1.8.3

+7
source share
2 answers

The error callback does not start due to state 202, but due to an error while parsing the response as JSON. For jQuery 2xx and 304 , success.

If the body of the Invalid Password response is invalid json and throws a jQuery error when it tries to parse it. The correct JSON string contains quotation marks around it, such as "Invalid Password" . You should JSON encode your responses with a JSON serializer, and not construct json manually, which you can see is error prone.

+9
source

Nevermind, he worked with

 statusCode: { 202: function() { console.log("Success!"); }, 403: function() { console.log("Error!'); } } 

Instead of success and error callbacks. Still a little perplexing 202 triggers an error though.

0
source

All Articles