I. Ajax process event analysis
Jquery triggers many events during Ajax execution.
These events can be divided into two types: local and global ).
Partial event: it can be called through $. Ajax. If an Ajax request does not want to generate a global event, you can set global: false.
Global events: similar to click events, they can be bound to each Dom element.
The trigger sequence of these events is described as follows:
| Local) |
Global events) |
|
AjaxstartGlobal events Start a new Ajax request, and no other Ajax request is in progress. |
BeforesendLocal events Triggered when an Ajax request starts. If necessary, you can set the xhr object here. |
AjaxsendGlobal events Global events triggered before the request starts. |
SuccessLocal events Triggered when the request is successful. That is, the server does not return errors, and the returned data is not. |
AjaxsuccessGlobal events The global request is successful. |
ErrorLocal events Triggered only when an error occurs. You cannot execute the success and error callback functions at the same time. |
AjaxerrorGlobal events Triggered when a global error occurs. |
CompleteLocal events Whether your request succeeds or fails, you can trigger this event when the request is complete even if the request is synchronized. |
AjaxcompleteGlobal events Triggered when a global request is complete. |
|
AjaxstopGlobal events Triggered when no Ajax is in progress. |
Note: Except for ajaxstart and ajaxstop, all other events have three parameters. Event, XMLHttpRequest, ajaxoptions The first is the event, the second is the xhr object, and the third parameter is the most useful when Ajax is called. For ajaxerror, there is also the fourth parameter thrownerror, which is passed only when an exception occurs. |
Ii. Examples of all Ajax process events
2.1. htmlCode
<Div>
<Input type = "button" onclick = "btnspareclick ();" value = "partevents"/>
<Input type = "button" onclick = "btnglobalclick ();" value = "globalevents"/>
</Div>
<Div id = "result"> result </div>
<Div id = "process"> Process </div>
2.2 jquery Ajax script
| Local instance |
Global Event instance |
| |
<Script language = "JavaScript" type = "text/JavaScript"> $. Ready (function btnglobalclick (){ $. Get ("http://www.jb51.net/windy2008/rss", {}, function (data, status, settings) {< BR >$ ("item", data ). each (function (I, domele) { $ ("# result "). append ("
" + $ (domele ). children ("title "). text () + "
"); }); $ ("# Process "). ajaxstart (function () { alert ($ (this ). text (); $ (this ). text ("Start new Ajax request"); }); $ ("# Process "). ajaxstop (function () { $ (this ). text ("when no Ajax is in progress"); alert ($ (this ). text (); }); $ ("# Process "). ajaxsend (function () { $ (this ). text ("Before Request start"); alert ($ (this ). text (); }); $ ("# Process "). ajaxsuccess (function () { $ (this ). text ("request succeeded"); alert ($ (this ). text (); }); $ ("# Process "). ajaxcomplete (function () { $ (this ). text ("when the request is completed"); alert ($ (this ). text (); }); $ ("# Process "). ajaxerror (function () { $ (this ). text ("request error"); alert ($ (this ). text (); }); |