We can append the Ajax global function to a specific Dom element. These functions will be triggered at different stages of Ajax request processing or when the request is ultimately successful or fails.
Any command syntax of the Ajax global function is consistent, so it is described in the following table.
|
Ajaxstart (callback)
Ajaxsend (callback)
Ajaxsuccess (callback)
Ajaxerror (callback)
Ajaxcomplete (callback)
Ajaxstop (callback) |
|
Attaches the passed callback function to all matching elements. Once the Ajax request is processed, the callback function is triggered. |
|
Parameters |
|
|
Callback |
(Function) will be appended with the callback function. Refer to the following table to learn when the callback function is triggered and what parameters will be passed. |
|
Return Value |
Package set |
Ajax global callback function (in the order of triggering)
|
Global Function Type |
When is triggered |
Parameters |
|
Ajaxstart |
When a jquery Ajax function or command is initiated, but before the xhr instance is created |
Global callback information object whose type is set to ajaxstart |
|
Ajaxsend |
After the xhr instance is created, but before the xhr instance is sent to the server |
Type is set to the global callback information object of ajaxsend; xhr instance; $. Ajax () function attribute |
|
Ajaxsuccess |
After the request has been returned from the server, and the response contains the success status code |
Type is set to the global callback information object of ajaxsuccess; xhr instance; $. Ajax () function attribute |
|
Ajaxerror |
After the request has been returned from the server, the response contains a failure status code. |
Global callback information object whose type is set to ajaxerror; xhr instance; $. Ajax () function attribute; Exception object returned by xhr instance (if any) |
|
Ajaxcomplete |
After the request has been returned from the server and any well-known ajaxsuccess or ajaxerror callback function has been called |
Global callback information object whose type is set to ajaxcomplete; xhr instance; attributes used by the $. Ajax () function |
|
Ajaxstop |
After all other Ajax processing and any other applicable global callback functions have been called |
Global callback information object whose type is set to ajaxstop |
Let's look at an example.
ClientCode:
<HTML xmlns = "http://www.w3.org/1999/xhtml">
Main server code:
Protected void page_load (Object sender, eventargs e) {If (! Page. ispostback) {If (request ["ID"]! = NULL &&! String. isnullorempty (request ["ID"]. tostring () {response. write (getdata (request ["ID"]. tostring () ;}} protected string getdata (string ID) {string STR = string. empty; Switch (ID) {Case "1": Str + = "this is number 1"; break; Case "2": Str + = "this is number 2 "; break; Case "3": Str + = "this is number 3"; break; default: Str + = "warning other number! "; Break;} return STR ;}
RunProgram, Result
We also confirmed the execution sequence of global functions.
Download demo