$. Ajax () method Parameter Parsing in jQuery, jquery. ajax

Source: Internet
Author: User

$. Ajax () method Parameter Parsing in jQuery, jquery. ajax

This article provides examples of jQuery $. ajax () method parameters for your reference. The specific content is as follows:

$. Ajax ({url: 'test. do ', data: {id: 123, name: 'xiaoming'}, type: 'post', ype: 'json', success: function (data) {alert (data); // pop-up window // TODO ........}, error: function (data) {alert (data); // pop-up window // TODO ........}})

Url: String-type parameter (the current page address by default.
Type: a parameter of the String type. The request method (post or 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: 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: The synchronous request locks the browser, and other user operations must be performed only after the request is completed.

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 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 type 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. 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. 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, error message, and captured error object (optional ).

Ajax event FunctionsAs follows:

Function (XMLHttpRequest, textStatus, errorThrown) {// generally, textStatus and errorThrown have only one of them containing 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 "application/x-www-form-urlencoded" by default ". 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 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: 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: 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 of 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 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. It is used to force charset modification only when the dataType is "jsonp" or "script" and the type is GET ). Generally, the local and remote content encoding is not used at the same time.

Case code:

$ (Function () {$ ('# send '). click (function () {$. ajax ({type: "GET", url: "test. json ", data: {username: $ (" # username "). val (), content: $ ("# content "). val ()}, dataType: "json", success: function (data) {$ ('# resText '). empty (); // clear all content in resText var html = ''; $. each (data, function (commentIndex, comment) {html + = ''+ comment ['username'] + ':

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.