$. Ajax () method parameters,. ajax method parameters

Source: Internet
Author: User

$. Ajax () method parameters,. ajax method parameters

It is very important to use ajax in a flexible manner. This article first introduces several ajax methods and analyzes them through examples. For details, see the following.

1. url:

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

2. 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.

3. timeout:

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

4. 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.

5. 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.

6. 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.

7. 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.

8. 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
}

9. 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
}

10. 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
}

11. 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 contain one of the information.
This; // The options parameter passed when calling this ajax request
}

12. 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.

13. 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;
}

14. 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;
}

15. 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.

16. 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.

17. 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.

18. username:

A String-type parameter is required. It is the username used to respond to the HTTP access authentication request.

19. password:

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

20. 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.

21. 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.

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 + = '<div class = "comment"> 

For example, the following content is displayed when you click tag a, which is implemented through ajax.

<script type="text/javascript">jQuery('.yd_content').load('yuding_tab0.html');jQuery.ajax({ url:"yuding_tab0.html", type:'get', data:{}, dataType:"html", success: function(data){  jQuery('.yd_content').html(data);  }, error: function(){  alert('error');  }});<script><script type="text/javascript">jQuery('.jiudian_content .tab li').bind('click', function(){ var _index=jQuery(this).index(); jQuery.ajax({  url:"yuding_tab"+_index+".html",  type:'get',  data:{},  dataType:"html",  success: function(data){  jQuery('.yd_content').html(data);  yuding();  },  error: function(){  alert('error');  }  }); })</script>

The above content is a complete ajax solution provided by the editor. I hope you will like it.

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.