admin管理员组

文章数量:1418303

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?

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?

Share Improve this question asked Jun 22, 2012 at 15:11 Brad KochBrad Koch 20.4k20 gold badges112 silver badges140 bronze badges 7
  • I think that when there is HTTP 200 response. – jcubic Commented Jun 22, 2012 at 15:13
  • 1 if it's not a 4xx/5xx code, basically. 3xx it should just follow the redirect then work with the code the new url returns, 2xx means http success anyways. – Marc B Commented Jun 22, 2012 at 15:13
  • I thinx 2xx and 3xx responses are success. But can't approve it. – Ruben Nagoga Commented Jun 22, 2012 at 15:14
  • 3 If the response is 2xx or 3xx AND the returned data matches the expected dataType and is valid, the response should be considered a success – Kevin B Commented Jun 22, 2012 at 15:16
  • 2 For those who can't see my answer (now deleted) it pointed out a line of code (if ( status >= 200 && status < 300 || status === 304 )) from the jQuery source. But there's more to it than just the status code, as @KevinB stated previously. – James Allardice Commented Jun 22, 2012 at 15:23
 |  Show 2 more ments

1 Answer 1

Reset to default 8

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 (monly happens in IE with invalid html/xml)

本文标签: javascriptWhat responses does jQueryajax consider to be quotsuccessquotStack Overflow