Ajax Events

Callbacks explained that jQuery.ajax() has four callback options: beforeSend, success, error, and complete. In addition to invoking these individually specified callback functions, jQuery’s ajax functions also fire custom events at each of the same stages in an Ajax request. The following table shows the callback options and the corresponding events:

CallbackEvent TypeHandler Registration Method
beforeSend“ajaxSend”ajaxSend()
success“ajaxSuccess”ajaxSuccess()
error“ajaxError”ajaxError()
complete“ajaxComplete”ajaxComplete()
“ajaxStart”ajaxStart()
“ajaxStop”ajaxStop()

You can register handlers for these custom Ajax events using the bind() method (see Advanced Event Handler Registration) and the event type string shown in the second column, or using the event registration methods shown in the third column. ajaxSuccess() and the other methods work just like the click(), mouseover(), and other simple event registration methods of Simple Event Handler Registration.

Since these are custom events generated by jQuery rather than the browser, the Event object passed to the event handler does not contain much useful detail. The “ajaxSend”, “ajaxSuccess”, “ajaxError”, and “ajaxComplete” events are all triggered with additional arguments, however. Handlers for these events will all be invoked with two extra arguments after the event. The first extra argument is the XMLHttpRequest object, and the second is the options object. This means, for example, ...

Get jQuery Pocket Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.