Recently, the Ajax function is often used in projects. After work, I searched for relevant information and summarized the use of Ajax methods and the meanings of parameters in the methods, for your reference
Type: a parameter of the string type. The request method (post or get) is get by default. Note other HTTP request methods, such as put and
Delete can also be used, but only some browsers support it.
Timeout: a parameter of the number type is required. Set the request timeout time (in milliseconds ). This setting overwrites the global settings of the $. ajaxsetup () method.
.
Async: A boolean parameter. The default value is true. All requests are asynchronous requests.
To send a synchronization request, set this option to false. Note: Synchronous requests will lock the browser and other user operations are required.
The request can be executed only after the request is complete.
Cache: A boolean parameter. The default value is true. When datatype is script, the default value is false ).
Setting false does not load request information from the browser cache.
Data: data sent to the server using an object or string parameter. If it is no longer a string, it is automatically converted to a string
. The GET request will be appended to the URL. To prevent this automatic conversion, you can view the processdata option. The object must be in the key/value format.
Format. For example, {foo1: "bar1", foo2: "bar2"} is converted to & foo1 = bar1 & foo2 = bar2. If it is an array, jquery will automatically be different
The value corresponds to the same name. For example, {FOO: ["bar1", "bar2"]} is converted to & Foo = bar1 & Foo = bar2.
Datatype: it must be a string type parameter. The data type returned by the server is expected. If this parameter is not specified, jquery automatically uses the http package mime
Responsexml or responsetext is returned and passed as the callback function parameter.
The available types are as follows:
XML: the XML document is returned and can be processed by jquery.
HTML: returns plain text HTML information. The included script tag is executed when the Dom is inserted.
Script: returns plain text JavaScript code. Results are not automatically cached. Unless the cache parameter is set. Note that in the remote request
All post requests are converted to GET requests.
JSON: Return JSON data.
Jsonp: jsonp format. When a function is called in the form of sonp, such as myurl? Callback = ?, Jquery will automatically Replace the last
"?" For the correct function name to execute the callback function.
Text: returns a plain text string.
Beforesend: a parameter of the function type. Before sending a request, you can modify the function of the XMLHTTPRequest object, for example, adding a custom
HTTP header. In beforesend, if false is returned, the Ajax request can be canceled. The XMLHTTPRequest object is the only parameter.
Number.
Function (XMLHttpRequest ){
This; // The options parameter passed when calling this Ajax request
}
Complete: a parameter of the function type. It is the callback function called after the request is complete (all calls are called when the request is successful or fails ).
Parameter: XMLHTTPRequest object and a string that describes the successful request type.
Function (XMLHttpRequest, textstatus ){
This; // The options parameter passed when calling this Ajax request
}
Success: Required parameter of the function type. The callback function called after the request is successful has two parameters.
(1) data returned by the server and processed according to the datatype parameter.
(2) A string describing the status.
Function (data, textstatus ){
// Data may be xmldoc, jsonobj, HTML, text, etc.
This; // The options parameter passed when calling this Ajax request
Error: Required parameter of the function type. The function called when the request fails. This function has three parameters: XMLHTTPRequest object and error
Optional ).
Ajax event functions are as follows:
Function (XMLHttpRequest, textstatus, errorthrown ){
// Generally, textstatus and errorthrown only contain one of the information.
This; // The options parameter passed when calling this Ajax request
}
Contenttype: String-type parameter. When the message is sent to the server, the content encoding type is default.
Is "application/X-WWW-form-urlencoded ". This default value is suitable for most applications.
Datafilter: a function that requires function-type parameters to pre-process the original data returned by Ajax.
Provides two parameters: Data and type. Data is the original data returned by Ajax, and type is provided when jquery. Ajax is called.
Datatype parameter. The value returned by the function will be further processed by jquery.
Function (data, type ){
// Return the processed data
Return data;
}
Global: A boolean parameter. The default value is true. Indicates whether to trigger a global Ajax event. Setting this parameter to false does not trigger global events.
Ajax events, such as ajaxstart or ajaxstop, can be used to control various Ajax events.
Ifmodified: A boolean parameter. The default value is false. Obtain New data only when the server data changes.
The server data change judgment is based on the last-modified header information. The default value is false, indicating that the header information is ignored.
Jsonp: a parameter of the string type. In a jsonp request, rewrite the name of the callback function.
This value is used to replace "Callback =? "Callback" in the URL parameter in this get or POST request, for example
{Jsonp: 'onjsonpload'} causes "onjsonpload =? "To the server.
Username: a string-type parameter that is used to respond to an HTTP access authentication request.
Password: a string-type parameter used to respond to an HTTP access authentication request.
Processdata: A boolean parameter. The default value is true. By default, the sent data is converted to an object (technically
It is not a string) to use with the default content type "application/X-WWW-form-urlencoded ". If you want to send Dom
Set the tree information or other information that you do not want to convert to false.
Scriptcharset: a string-type parameter. Only when the datatype is "jsonp" or "script" and the type is get
Is used to forcibly modify the character set (charset ). Generally, the local and remote content encoding is not used at the same time.
Sample Code:
$. Ajax ({type: "Get", // request type get, post, default get URL: "/help/index? Prtid = "+ OBJ, // request sending Address Timeout:" ", // request timeout time in milliseconds async: True, // whether the request is asynchronous or synchronous, the default asynchronous true cache: true, // The default value is true. When datatype is set to script, if it is set to false by default, the request information datatype: "" will not be loaded from the browser cache, // The return type is XML, HTML, script, JSON, text, complete: function () {}, // call this function after the request is complete data: OBJ, // parameter GET request: response. querystring [""] POST request: response. queryform [""] // the data sent to the server by a parameter of the object or string type. If it is no longer a string, it is automatically converted to a string. The GET request will be appended to the URL. To prevent this automatic conversion, you can view the processdata option. The object must be in the key/value format. For example, {foo1: "bar1", foo2: "bar2"} is converted to & foo1 = bar1 & foo2 = bar2. If it is an array, jquery automatically corresponds to the same name for different values. For example, {FOO: ["bar1", "bar2"]} is converted to & Foo = bar1 & Foo = bar2. Success: function () {alert ("success") ;}, // after the request is successful, the called function error: function () {alert ("error ");}, // when the request fails, the called function contenttype: "", // Content Encoding type when the information is sent to the server. The default value is "application/X-WWW-form-urlencoded ", this is suitable for most applications by default. Global: True, // whether to trigger global Ajax events. The default value is true ifmodified: false. // jsonp :"", // It must be a string type parameter, and the name of the callback function is rewritten in a jsonp request. This value is used to replace "Callback =? "Callback" in the URL parameter of this get or POST request, for example, {jsonp: 'onjsonpload'} causes "onjsonpload =? "To the server. Username: "", // a string-type parameter, which is the username used to respond to an HTTP access authentication request. Password: "", // a string-type parameter, used to respond to an HTTP access authentication request password. Processdata: True, // The default value is true. The sent data is converted to an object instead of a string to be used with the default encoding type "application/X-WWW-form-urlencoded ", ". If you want to send DOM tree information or other information that does not want to be converted, set it to false. Scriptcharset: "" // only when the datatype is "jsonp" or "script" and the type is get, it is used to forcibly modify the character set (charset ). Generally, the local and remote content encoding are not used at the same time}); and: $. get (URL, Data, success, datatype) is equivalent to $. ajax ({URL: URL, data: data, success: Success, datatype: datatype });
Attached: $. Each () function:
$. The each () function is different from the each () method of the jquery object. It is a global function that does not operate the jquery object, but uses an array or object as 1st parameters, take the number of callbacks as 2nd parameters. The callback function has two parameters: 1st are the indexes of the object's members or arrays, and 2nd are the corresponding variables or content.