Detailed parsing of parameters in the Jquery.ajax () method

Source: Internet
Author: User

Objective

In project development, the Jquery.ajax method is most often used in order to implement asynchronous requests to the server. At first, the requirements are relatively simple, the parameters to be passed when calling the Jquery.ajax method are the common parameters: Url/data/datatype/type/cache/timeout. I felt that this interface was very simple to use, until a recent project to implement a request to send Formdata data to the server, I found myself wrong .... The original Jquery.ajax method in addition to the ones I used, there are so many!! So I decided to go to the jquery website and have a good look at the parameters I don't know!

Parameter resolution

The parameters in the Jquery.ajax () method can be divided into 2 categories:

One type is the parameters related to the Ajax events (local events) handler function: including Success/error/beforesend/complete, these parameters are better understood, this article will not be discussed, if interested, please refer to my other article " A summary of AJAX related events in jquery;

The other is related to the AJAX request parameters, which I would like to focus on this article and everyone to resolve the parameters.
Note: These parameters are optional and jquery uses the default values for these parameters if you do not manually set them yourself.

1. URL

Default value: The URL of the current page
Type: String (String)
Meaning: The destination address (server side address) of the AJAX request sent

2. Type

Default value: ' GET '
Type: String (String)
Meaning: The requested way, the desirable value is "POST", "GET", "PUT", "HEADER" and so on

3. Data

Default value: None
Type: Plainobject or String or Array
Meaning: Data sent to the server. Regardless of the data value why type,
If you do not want jquery to be converted automatically, you can do so by setting Processdata=false.

4, Datafilter

Type: function (Data,type)
Meaning: Further filtering processing of response data returned by the server

5. Accept

Default value: The value of the datatype parameter
Type: Plainobject (Simple Object)
Meaning: The client tells the server that it can receive the type of response data itself

6. Async

Default value: True
Type: Boolean (Boolean)
Meaning: Indicates whether the AJAX request is asynchronous or synchronous. Asynchronously indicates that once this request is made
The synchronization indicates that after the request is made, the program pauses to wait for the service-side response, and the subsequent code is blocked.
The subsequent code is not resumed until the service-side response has been received.
You only need to set Async to False, but because this usage will block the browser, it is not recommended;

Note: Synchronous AJAX requests are not supported in cross-domain AJAX requests and DATATYPE:JSONP AJAX requests

7. Cache

Default value: True (but when datatype is ' jsonp ' or ' script ', the cache default value will be false)
Type: Boolean (Boolean)
Meaning: When the cache is false, the browser will not cache pages that are requested back. Of course, if you request the ' Jsonp ' or ' script ', the browser will not be cached by default.

Note: It is only valid if the cache is set to false for Ajax requests of type Header/get.
(This parameter is not required for other types of Ajax requests??? )

8, ContentType:

Default value: ' Application/x-www-form-urlencoded;charset=utf-8 '
Type: Boolean or String (Boolean/strings)
Meaning: This parameter is used when the client sends data to the server (including Get, post??). ContentType tells the server-side ' the type of data sent by the client ' to help the server parse the data. If the data you send is not the type data specified by ContentType, jquery automatically converts the data to the type specified by ContentType. Setting contenttype to False,jquery will not add the ContentType field to the HTTP header, the data will need to be set to false contenttype)

Attention:
1. If the parameter is explicitly set, the AJAX request will be sent regardless of the data.
2, the client sends the data charset can only be UTF-8, even if you change to other values, it will still use UTF-8.
3, for cross-domain requests, when ContentType is not ' application/x-www-form-urlencoded ', ' multipart/form-data ', or ' text/plain ', the browser will first send a The OPTIONS request to the server before sending the AJAX request that we want to send.

9, DataType

Default value: None
Type: string literal
Meaning: The type of data you want to receive, if the parameter is explicitly set in the AJAX request, but the data type returned by the server is not the type specified by datatype, jquery automatically formats the data.

The value of datatype directly affects the data parameter of the Success success callback function:
Datatype= ' xml ':d ATA will be XML Document and can be manipulated directly using the DOM API for data
Datatype= ' HTML ':d ATA is plain text and cannot be manipulated directly using the DOM API for data
Datatype= ' JSON ':d ATA as a JavaScript object. If the returned data is empty, it will be an error, and when no data is returned, it should return null or {}
Datatype= ' Jsonp ': Similar to ' JSON ' (to be added)
Datatype= ' script ':d ata for plain text
Datatype= ' text ':d ata for plain text

You will find that there are 2 types of data returned: XML and Text (JSON-type data is also string literals), so the response of the AJAX request is divided into 2 types: ResponseText and Responsexml

10. Global

Default value: True
Type: Boolean Boolean value
Meaning: Whether the AJAX request will trigger an AJAX global event such as Ajaxstop/ajaxstart.

11, headers

Default value: {}
Type: Plainobject Simple Object
Meaning: Add an extra header field to the request header.
Note: The header field added by this parameter can be overridden in the Beforesend method

12, Ifmodified

Default value: False
Type: Boolean Boolean value
Meaning: When this parameter is False, jquery does not check the last-modified and ETag fields of the response header, and when True, jquery checks the 2 fields to see if the data changes and the success callback is triggered.

13, ProcessData

Default value: True
Type: Boolean Boolean value
Meaning: If this parameter is set to True, jquery will handle the value of the data parameter as the format type specified by the contenttype parameter. The default value for contenttype is ' application/x-www-form-urlencoded;charset=utf-8 ', so when the data parameter value is an object or array type, jquery will use the data The data is automatically converted to data that conforms to the ' application/x-www-form-urlencoded;charset=utf-8 ' format (that is, query string type). When you want to send DOMDocument or formdata, you should set the ProcessData to False to avoid jquery's automatic format conversion processing of data.

14, StatusCode

Default value: {}
Type: Plainobject
Meaning: A callback function is defined for each HTTP response status code. Examples are as follows:

$.ajax({  statusCode: {   404: function() {     alert( "page not found" );   }  
15. TimeOut

Default value: None
Type: Number (unit: milliseconds)
Meaning: Defines the time that the AJAX request timed out. From the beginning of the AJAX request, to the AJAX request is actually sent out. If this stage time exceeds the value set by timeout, the Ajax error event is triggered.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Detailed parsing of parameters in the Jquery.ajax () method

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.