Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery.ajax() success/failure callbacks called when?

I've been going through the source to find out the critiera for jQuery.ajax()'s success/failure methods being called. It is not based solely on the status code, it seems to also involve the data type.

I always resort to writing custom error handlers using the 'complete'-callback.

Exactly which are the critera for the success/failure calls?

like image 436
bjornl Avatar asked Oct 25 '10 09:10

bjornl


People also ask

When AJAX success is called?

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 is callback jQuery AJAX?

jQuery - ajaxSuccess( callback ) Method The ajaxSuccess( callback ) method attaches a function to be executed whenever an AJAX request completes successfully. This is an Ajax Event.

Which method is used on the returned object of AJAX () method if the AJAX call fails?

If an AJAX request fails, you can react to the failure inside the callback function added via the fail() function of the object returned by the $. ajax() function. Here is a jQuery AJAX error handling example: var jqxhr = $.

What is success response AJAX?

Response is the object passed as the first argument of all Ajax requests callbacks. This is a wrapper around the native xmlHttpRequest object. It normalizes cross-browser issues while adding support for JSON via the responseJSON and headerJSON properties.


1 Answers

As you said, it depends on the data type, script is a special one for instance, the check is:

  • Has the request already completed? (don't fire twice)
  • Is the readyState "loaded" or "complete"?

For other requests it's checks the following:

  • Is it a timeout?
  • Does jQuery.httpSuccess() return true?

    • Is the status set?
    • Is the response code between 200-299?
    • Is the response code 304 or 1223?
  • Is it modified? (do we care about the [not] updated result?)

Note: The above is for jQuery 1.4.3, jQuery 1.4.2 and below had an additional "success" scenario where a response code of 0 was also "successful", this was done because Opera returns a 0 when it's really a 304. This is incorrect behavior, and the jQuery team opted to drop support for this quirk, since it caused false-positives in other actual 0 response code cases.

like image 72
Nick Craver Avatar answered Oct 05 '22 23:10

Nick Craver