In-depth analysis of $. ajax () and. ajax

Source: Internet
Author: User

In-depth analysis of $. ajax () and. ajax

Url:

It must be a string parameter (the current page address by default.

Type:

The request method (post live get) is get by default. Note that other http request methods, such as put and delete, can also be used, but are only supported by some browsers.

Timeout:

Set the request timeout time (in milliseconds) for a Number parameter ). This setting overwrites the global settings of the $. ajaxSetup () method.

Async:

A Boolean parameter is required. The default value is true. All requests are asynchronous requests. To send a synchronization request, set this option to false. Note: The synchronous request locks the browser. Other operations can be performed only after the request is completed.

Cache:

A Boolean parameter is required. The default value is true. If dataType is set to script, the default value is false. If it is set to false, request information is not loaded from the browser cache.

Data:

The data sent to the server must be of the Object or String type. If it is no longer a string, it is automatically converted to the string format. 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.

DataType:

It must be a String parameter. The data type returned by the server is expected. If this parameter is not specified, JQuery will automatically return responseXML or responseText Based on the mime information of the http package and pass it as a 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 during remote requests (not in the same domain), all post requests will be 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 is required. 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 a unique parameter.

Function (XMLHttpRequest) {this; // The options parameter passed when calling this ajax request} complete:

A parameter of the Function type is required. The callback Function called after the request is complete (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:

A parameter of the Function type is required. 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, and so on this; // call the options parameter passed in this ajax request} error:

A parameter of the Function type is required. The Function called when the request fails. This function has three parameters: XMLHttpRequest object, error message, and captured error object (optional ). Ajax event functions are as follows:

Function (XMLHttpRequest, textStatus, errorThrown) {// generally, textStatus and errorThrown only have one of the following information: this; // The options parameter passed when calling this ajax request} contentType:

It must be a String parameter. When the message is sent to the server, the content encoding type is "application/x-www-form-urlencoded" by default ". This default value is suitable for most applications.

DataFilter:


A Function that requires a Function type parameter 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 the dataType parameter provided when jQuery. ajax is called. The value returned by the function will be further processed by jQuery.

Function (data, type) {// return the processed data return data;} global:

It must be a Boolean parameter. The default value is true. Indicates whether to trigger a global ajax event. Setting false does not trigger global ajax events. ajaxStart or ajaxStop can be used to control various ajax events.

IfModified:

It must be 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 is required. 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 is required. It is the username used to respond to the HTTP access authentication request.

Password:

It must be a String type parameter, used to respond to the password of the HTTP access authentication request.

ProcessData:

It must be a Boolean parameter. The default value is true. By default, the sent data will be converted to an object (technically not a string) to work with the default content 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:

A parameter of the String type is required. It is used to force charset modification only when dataType is "jsonp" or "script" and type is GET ). Generally, the local and remote content encoding is not used at the same time.

The above in-depth analysis of the $. ajax () method is all the content shared by the editor. I hope to give you a reference and support for the help house.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.