English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
AJAX requests generate many different events that you can subscribe to.
There are two types of AJAX events:
Local events
Global events
AJAX local events are callbacks that can be subscribed to within the AJAX request object.
You can listen to local AJAX events like this:
$.ajax({ beforeSend: function(){ // Handle beforeSend event }, error: function(){ // Handle error event }, success: function(){ // Handle success event }, complete: function(){ // Handle complete event } $("#done").append(event.type);});/Test to see‹
There are four local AJAX events:
beforeSend-Functions to run before sending the request
error -Function to run if the request fails
success -Functions to run when the request is successful
complete-CompleteFunctions to run after the request (after success and error functions)
AJAX global events are triggered on the document, calling any handlers that are listening.
You can listen to global AJAX events like this:
$("button").click(function(){ $("#result").load("ajax_intro.txt"); $("#done").append(event.type); $(document).ajaxComplete(function(event, xhr, options){ $("#done").append(options.url); $("#done").append(options.url); $("#done").append(xhr.status); $("#done").append(event.type);});/Test to see‹
›
There are six Ajax global events: -ajaxComplete()
The function to be called when an Ajax request is completed -ajaxError()
The function to be called when an Ajax request is completed and an error is attached -ajaxSend()
The function to be called before sending an Ajax request -ajaxStart()
The function to be called before the first Ajax request starts -ajaxStop()
The function to be called after all Ajax requests are completed -ajaxSuccess()
The function to be called upon successful completion of each Ajax requestFor a complete reference of AJAX methods, please visit ourjQuery AJAX Reference