This article focuses on the global event reference method for Ajax and the sequence of execution of each event, so let's read this article together.
All global events for the Ajax method of jquery:
Ajaxstart:ajax before the request begins
Ajaxsend:ajax when requested
Ajaxsuccess:ajax after getting the data
Ajaxcomplete:ajax when the request is complete
Ajaxerror:ajax after an error has occurred on the request
Ajaxstop:ajax after a request is stopped
The use of global events for AJAX methods
When you use jquery's ajax approach, both $.ajax (), $.get (), $.load (), $.getjson (), and so on, will trigger global events by default, but they are usually not bound to global events, but these global events are actually very useful.
Ajax global events, there is a typical application: your page has multiple or even a number of Ajax requests, but these AJAX requests have the same message mechanism. A prompt is displayed before the AJAX request begins, prompting "reading data", and the Prompt box displays "Data acquisition success" when the Ajax request succeeds, and hides the prompt after the AJAX request ends. The practice of not using global events is to add the $.ajax () plus the beforesend, success, and complete callback functions to the callback function with the Processing hints box. The practice of using global events is:
$ (document). Ajaxstart (OnStart). ajaxcomplete (OnComplete) . ajaxsuccess (onsuccess); function OnStart (event ) {//.....} function OnComplete (event, XHR, Settings) {//...} function Onsuccess (event, XHR, Settings) {//...}
Each event in jquery is executed in the following order
1.ajaxStart (Global event)
2.beforeSend (Local event)
3.ajaxSend (Global event)
4.success (Local event)
5.ajaxSuccess (Global event)
6.error (Local event)
7.ajaxError (Global event)
8.complete (Local event)
9.ajaxComplete (Global event)
10.ajaxStop (Global event)
Example
<! DOCTYPE html>