The parameters of $.ajax () method in jquery and its application

Source: Internet
Author: User
Tags character set http request json requires script tag browser cache

 jquery $.ajax () method presumably everyone is not unfamiliar, in this article will introduce its parameters and application examples, interested friends do not miss

URL: The requested address is sent for a string type parameter, which defaults to the current page address.     Type: A parameter of type string is required, and the request method (post or get) defaults to get. Note that other HTTP request methods, such as put and     Delete, are also available, but are supported by only some browsers.     Timeout: Requires a parameter of type number to set the request timeout (in milliseconds). This setting overrides the global setting     of the $.ajaxsetup () method.     Async: A parameter of Boolean type is required, the default setting is true, and all requests are asynchronous requests.     Set this option to False if you need to send a sync request. Note that the synchronization request will lock the browser, and the user's other actions must wait for the     to be completed before they can be executed.     Cache: Parameter of Boolean type, default is True (default is False when datatype is script).     set to false will not load request information from the browser cache.     Data: Requires an object or string type of parameter to be sent to the server. If it is no longer a string, it is automatically converted to a string    . The GET request is appended to the URL. To prevent this automatic conversion, you can view the ProcessData option. The object must be Key/value    , for example {foo1: "Bar1", Foo2: "Bar2"} converted to &foo1=bar1&foo2=bar2. If it is an array, jquery will automatically correspond to the same name for different     values. For example, {foo:["bar1", "Bar2"]} is converted to &FOO=BAR1&FOO=BAR2.     DataType: A parameter of type string that expects the data type returned by the server. If not specified, jquery automatically returns Responsexml or ResponseText based on the HTTP packet mime    information and is passed as a callback function parameter.     available types are as follows:    XML: Returns an XML document that can be jqueryProcessing.     HTML: Returns plain text HTML information, and the included script tag executes when the DOM is inserted.     Script: Returns the plain text JavaScript code. Results are not automatically cached. Unless the cache parameter is set. Note that at remote request     (not in the same domain), all post requests are converted to get requests.     JSON: returns JSON data.     JSONP:JSONP format. When calling a function using the Sonp form, for example, Myurl?callback=?,jquery will automatically replace the latter     "?" To the correct function name to execute the callback function.     Text: Returns a plain text string.     Beforesend: A parameter that requires a function type to modify the functions of the XMLHttpRequest object before sending the request, such as adding custom     HTTP headers. If you return False in Beforesend, you can cancel this Ajax request. The XMLHttpRequest object is the only parameter     number.     Function (XMLHttpRequest) {    this;/////////////////////////////// Complete: A parameter that requires a function type that is invoked after the request completes (called when the request succeeds or fails).     Parameters: XMLHttpRequest object and a string that describes the type of successful request.     Function (XMLHttpRequest, textstatus) {    this;/////////////////////////////// nbsp Success: A parameter that requires a function type, and a callback function called after the success of the request, with two parameters.     (1) The data returned by the server and processed according to the datatype parameter.     (2) describes the status of a string.     Function (data, textstatus) {&nbsP  //data may be xmldoc, jsonobj, HTML, text, and so on     this; The options parameter     Error that is passed when invoking this AJAX request: A parameter that requires a function type, and called when the request fails. The function has 3 parameters, that is, the XMLHttpRequest object, the wrong     error message, and the object of the trap (optional).     AJAX event functions are as follows:    function (XMLHttpRequest, Textstatus, Errorthrown) {   // Usually only one of the Textstatus and Errorthrown contains information     this; Options parameters passed when invoking this Ajax request    }    ContentType: parameter of type string, when sending information to the server, the content encoding type default     is " Application/x-www-form-urlencoded ". This default value is appropriate for most applications.     Datafilter: A function that requires preprocessing of raw data returned by Ajax for parameters of function type.     Provides data and type two parameters. Data is the original of the Ajax return, type is the     datatype parameter provided when the Jquery.ajax is invoked. The value returned by the function will be further processed by jquery.     Function (data, type) {   //back to processed data     return data;   }    Global: To is a Boolean type argument, and the default is true. Indicates whether global AJAX events are triggered. Setting to FALSE will not trigger global     AJAX events, Ajaxstart or ajaxstop can be used to control various AJAX events.     Ifmodified: Parameter of Boolean type is required, default is False. Gets new data only when the server data changes.     Server data change judgment is based on last-modified header information. The default value is False, which is to ignore header information.     JSONP: Requires a string-type parameter to override the name of the callback function in a JSONP request.     This value is used in place of the "callback=?" The "callback" section of the URL parameter in this get or POST request, such as     {jsonp: ' onjsonpload '}, causes the "onjsonpload=?" passed to the server.     Username: A parameter of type string that is required to respond to the user name of an HTTP access authentication request.     Password: A parameter of type string that is required to respond to the password of an HTTP access authentication request.     ProcessData: A parameter of Boolean type is required, and the default is true. By default, the data sent is converted to an object (not a string from a technical perspective    ) to match the default content type "application/x-www-form-urlencoded". Set to False if you want to send dom    tree information or other information that you do not want to convert.     Scriptcharset: Requires a parameter of type string, only when the request is datatype "JSONP" or "script", and the type is get when     is used to force the character set to be modified ( CharSet). Typically used when local and remote content encodings are different.         Case code:  code is as follows: $ (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 contents of ResText     var html = ';    $.each ( Data, function (Commentindex, comment) {    HTML + + ' <div class= ' comment ' ><h6> ' + comment[' Username ']    + ': </h6><p class= "para" ' + comment[' content ']    + ' </p></div> ' ;   });    $ (' #resText '). HTML (HTML);   }   });   });   } ;    by the way $.each () function:    $.each () function is different from each () method of the JQuery object, it is a global function and does not manipulate the jquery object. Instead, take an array or object as the 1th argument, with a callback function as the 2nd argument. The callback function has two parameters: The 1th is the object's member or the index of the array, and the 2nd is the corresponding variable or content.  
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.