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?
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? 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.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With