Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 379
Brad Koch Avatar asked Jun 22 '12 15:06

Brad Koch


People also ask

What is success in jQuery AJAX?

What is AJAX success? AJAX success is a global event. Global events are triggered on the document to call any handlers who may be listening. The ajaxSuccess event is only called if the request is successful. It is essentially a type function that's called when a request proceeds.

What triggers AJAX success?

What triggers Ajax success? Whenever an Ajax request completes successfully, jQuery triggers the ajaxSuccess event. Any and all handlers that have been registered with the . ajaxSuccess() method are executed at this time.

What is success and error in AJAX?

jquery ajax calls are asynchronous by default. So success & error functions will be called when the ajax load is complete. But your return statement will be executed just after the ajax call is started.

What is difference between success and complete in AJAX?

success() only gets called if your webserver responds with a 200 OK HTTP header - basically when everything is fine. However, . complete() will always get called no matter if the ajax call was successful or not - maybe it outputted errors and returned an error - . complete() will still get called.


1 Answers

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)

like image 197
Kevin B Avatar answered Sep 21 '22 16:09

Kevin B