Detailed introduction to the jqueryajax method and parameters

Source: Internet
Author: User
This article shares the use of jqueryajax methods and detailed descriptions of jquery ajax methods and parameters.

1. $. ajax () has only one parameter: The parameter key/value object, which contains information about various configurations and callback functions.

Parameter List:

Parameter Name Type Description
Url String (Default: Current page address) the address of the request sent.
Type String (Default: "GET") Request Method ("POST" or "GET"). The default value is "GET ". Note: Other HTTP request methods, such as PUT and DELETE, can also be used, but are only supported by some browsers.
Timeout Number Set the request timeout (in milliseconds ). This setting overwrites the global setting.
Async Boolean (Default: true) by default, 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.
BeforeSend Function Before sending a request, you can modify the function of the XMLHttpRequest object, for example, adding a custom HTTP header. The XMLHttpRequest object is a unique parameter.
function (XMLHttpRequest) {         this; // the options for this ajax request         }
Cache Boolean (Default: true) New jQuery 1.2 function. setting this parameter to false will not load request information from the browser cache.
Complete Function Callback Function after the request is complete (called when the request is successful or fails ). Parameter: XMLHttpRequest object, success information string.
function (XMLHttpRequest, textStatus) {         this; // the options for this ajax request         }
ContentType String

(Default: "application/x-www-form-urlencoded") Content Encoding type when sending information to the server. The default value is applicable to most applications. Tell the server the format of the data submitted from the browser.

For example, if the JSON. stringify (obj) method in JSON2.js is used for data submission and is formatted as a json string, an error will be reported if it is submitted by default. In this case, you must specify the format of the submitted content as "application/json ".

Data Object,
String

The data sent to the server.

If the data type is JavaScript Object or array, Jquery automatically calls JQuery before submission. the param () method encodes the data to be sent into "application/x-www-form-urlencoded" format (name = value & name1 = value1 ); javaScript objects must be in the Key/Value format. If they are arrays, jQuery automatically corresponds to the same name for different values. For example, {foo: ["bar1", "bar2"]} is converted to '& foo = bar1 & foo = bar2 ';

If the data type is String, the data is directly encoded in the "application/x-www-form-urlencoded" format by default and no conversion is performed.

The processData option controls whether to convert the data. The default value is true.

DataType String

Expected data type returned by the server. Set the content of the "Accept" field in the HttpHeader to tell the server browser that the data format type can be returned. JQuery also processes the returned data based on the type. 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. Available values:

"Xml": returns an XML document, which can be processed by jQuery.

"Html": returns plain text HTML information, including script elements.

"Script": returns plain text JavaScript code. Results are not automatically cached.

"Json": Return JSON data. JQuery automatically converts the returned string format data to a Javascript Object to facilitate direct access in the obj. property format. If this option is not specified, JQuery will not automatically convert the returned string in JSON format.

"Jsonp": JSONP format. When calling a function in the form of JSONP, such as "myurl? Callback =? "Will jQuery be replaced automatically? For the correct function name to execute the callback function.

Error Function (Default: automatically determines (xml or html) This method is called when a request fails. This method has three parameters: XMLHttpRequest object, error message, and (possibly) captured error object.
Function (XMLHttpRequest, textStatus, errorThrown) {// Generally, only one of textStatus and errorThown has the value this; // the options for this ajax request}
Global Boolean (Default: true) Whether to trigger a global AJAX event. Setting false does not trigger global AJAX events, such as ajaxStart or ajaxStop. Can be used to control different Ajax events
IfModified Boolean (Default: false) obtain new data only when the server data changes. Use the Last-Modified header information of the HTTP packet to determine.
ProcessData Boolean (Default: true) by default, data sent is converted to an object (technically not a string) with the default content type "application/x-www-form-urlencoded ". If you want to send the DOM tree information or other information that does not want to be converted, set it to false.
Success Function Callback Function after successful request. This method has two parameters: the server returns data and returns the status.
function (data, textStatus) {         // data could be xmlDoc, jsonObj, html, text, etc...         this; // the options for this ajax request         }

2. jQuery. get(Url, [data], [callback], [type]): Use the GET Method for asynchronous requests.

Parameters:
Url (String): the URL of the request.
Data (Map): (optional) data to be sent to the server, expressed in Key/value pairs.
Callback (Function): (optional) callback Function when the load is successful (this method is called only when the Response returns success ).
Type (String): (optional) the official description is: Type of data to be sent. In fact, it should be the client request type (JSON, XML, and so on)

3. jQuery. post(Url, [data], [callback], [type]): Use the POST method for asynchronous requests.

Parameters:
Url (String): the URL of the request.
Data (Map): (optional) data to be sent to the server, expressed in Key/value pairs.
Callback (Function): (optional) callback Function when the load is successful (this method is called only when the Response returns success ).
Type (String): (optional) the official description is: Type of data to be sent. In fact, it should be the client request type (JSON, XML, and so on)

1. $. ajax () has only one parameter: The parameter key/value object, which contains information about various configurations and callback functions.

Parameter List:

Parameter Name Type Description
Url String (Default: Current page address) the address of the request sent.
Type String (Default: "GET") Request Method ("POST" or "GET"). The default value is "GET ". Note: Other HTTP request methods, such as PUT and DELETE, can also be used, but are only supported by some browsers.
Timeout Number Set the request timeout (in milliseconds ). This setting overwrites the global setting.
Async Boolean (Default: true) by default, 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.
BeforeSend Function Before sending a request, you can modify the function of the XMLHttpRequest object, for example, adding a custom HTTP header. The XMLHttpRequest object is a unique parameter.
function (XMLHttpRequest) {         this; // the options for this ajax request         }
Cache Boolean (Default: true) New jQuery 1.2 function. setting this parameter to false will not load request information from the browser cache.
Complete Function Callback Function after the request is complete (called when the request is successful or fails ). Parameter: XMLHttpRequest object, success information string.
function (XMLHttpRequest, textStatus) {         this; // the options for this ajax request         }
ContentType String

(Default: "application/x-www-form-urlencoded") Content Encoding type when sending information to the server. The default value is applicable to most applications. Tell the server the format of the data submitted from the browser.

For example, if the JSON. stringify (obj) method in JSON2.js is used for data submission and is formatted as a json string, an error will be reported if it is submitted by default. In this case, you must specify the format of the submitted content as "application/json ".

Data Object,
String

The data sent to the server.

If the data type is JavaScript Object or array, Jquery automatically calls JQuery before submission. the param () method encodes the data to be sent into "application/x-www-form-urlencoded" format (name = value & name1 = value1 ); javaScript objects must be in the Key/Value format. If they are arrays, jQuery automatically corresponds to the same name for different values. For example, {foo: ["bar1", "bar2"]} is converted to '& foo = bar1 & foo = bar2 ';

If the data type is String, the data is directly encoded in the "application/x-www-form-urlencoded" format by default and no conversion is performed.

The processData option controls whether to convert the data. The default value is true.

DataType String

Expected data type returned by the server. Set the content of the "Accept" field in the HttpHeader to tell the server browser that the data format type can be returned. JQuery also processes the returned data based on the type. 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. Available values:

"Xml": returns an XML document, which can be processed by jQuery.

"Html": returns plain text HTML information, including script elements.

"Script": returns plain text JavaScript code. Results are not automatically cached.

"Json": Return JSON data. JQuery automatically converts the returned string format data to a Javascript Object to facilitate direct access in the obj. property format. If this option is not specified, JQuery will not automatically convert the returned string in JSON format.

"Jsonp": JSONP format. When calling a function in the form of JSONP, such as "myurl? Callback =? "Will jQuery be replaced automatically? For the correct function name to execute the callback function.

Error Function (Default: automatically determines (xml or html) This method is called when a request fails. This method has three parameters: XMLHttpRequest object, error message, and (possibly) captured error object.
Function (XMLHttpRequest, textStatus, errorThrown) {// Generally, only one of textStatus and errorThown has the value this; // the options for this ajax request}
Global Boolean (Default: true) Whether to trigger a global AJAX event. Setting false does not trigger global AJAX events, such as ajaxStart or ajaxStop. Can be used to control different Ajax events
IfModified Boolean (Default: false) obtain new data only when the server data changes. Use the Last-Modified header information of the HTTP packet to determine.
ProcessData Boolean (Default: true) by default, data sent is converted to an object (technically not a string) with the default content type "application/x-www-form-urlencoded ". If you want to send the DOM tree information or other information that does not want to be converted, set it to false.
Success Function Callback Function after successful request. This method has two parameters: the server returns data and returns the status.
function (data, textStatus) {         // data could be xmlDoc, jsonObj, html, text, etc...         this; // the options for this ajax request         }

2. jQuery. get(Url, [data], [callback], [type]): Use the GET Method for asynchronous requests.

Parameters:
Url (String): the URL of the request.
Data (Map): (optional) data to be sent to the server, expressed in Key/value pairs.
Callback (Function): (optional) callback Function when the load is successful (this method is called only when the Response returns success ).
Type (String): (optional) the official description is: Type of data to be sent. In fact, it should be the client request type (JSON, XML, and so on)

3. jQuery. post(Url, [data], [callback], [type]): Use the POST method for asynchronous requests.

Parameters:
Url (String): the URL of the request.
Data (Map): (optional) data to be sent to the server, expressed in Key/value pairs.
Callback (Function): (optional) callback Function when the load is successful (this method is called only when the Response returns success ).
Type (String): (optional) the official description is: Type of data to be sent. In fact, it should be the client request type (JSON, XML, and so on)

The above is a detailed description of the jquery ajax method and parameters. For more information, see other related articles in the first PHP community!

Related Article

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.