Brad Koch
Brad Koch

Reputation: 20337

What responses does jQuery.ajax consider to be "success"?

I have a jQuery AJAX post request that is unexpectedly triggering the error callback instead of success. One random suspicion I have is the 302 status code it is receiving, although that may be wrong.

I looked at the documentation, but I feel like one thing is a bit unclear: What is jQuery's definition of a successful request?

Upvotes: 9

Views: 2061

Answers (1)

Kevin B
Kevin B

Reputation: 95059

If the response is between 199 and 300 ( >= 200 and < 300 ) or equal to 304 and the responseText can be successfully converted to the dataType that you provide (text by default), it is considered a successful request.

For example, if you return JSON and you get a 200 response status but it fails, it is more than likely a JSON parser problem meaning your JSON is not valid.

If you are returning HTML or XML and it fails with a 200 response status, the responsetext couldn't be converted to HTML or XML respectively (commonly happens in IE with invalid html/xml)

Upvotes: 8

Related Questions