ajax method in jquery

The parameters of the ajax method in jquery are always forgotten, so record them here.


 


1.url: 
A parameter of type String is required, (default is the address of the current page) the address where the request is sent.


2.type: 
The parameter is required to be of type String. The request method (post or get) defaults to get. Note that other http request methods such as put and delete can also be used, but are only supported by some browsers.


3.timeout: It 
is required to be a parameter of type Number, and set the request timeout time (milliseconds). This setting will override the global setting of the $.ajaxSetup() method.


4. async: 
The parameter is required to be a Boolean type. The default setting is true, and all requests are asynchronous requests. Set this option to false if you need to send synchronous requests. Note that a synchronous request will lock the browser, and the user must wait for the request to complete before other operations can be performed.


5.cache: 
The parameter is required to be a Boolean type. The default is true (when the dataType is script, the default is false). If set to false, the request information will not be loaded from the browser cache.


6.data: 
Requires a parameter of type Object or String, the data sent to the server. If it is not a string, it will be automatically converted to string format. The get request will be appended to the url. To prevent this automatic conversion, look at the processData option. The object must be in key/value format, e.g. {foo1:"bar1",foo2:"bar2"} is converted to &foo1=bar1&foo2=bar2. If it is an array, JQuery will automatically assign different values ​​to the same name. For example {foo:["bar1","bar2"]} is converted to &


7. dataType: 
The parameter is required to be of type String, and the data type returned by the server is expected. If not specified, JQuery will automatically return responseXML or responseText according to the http package mime information, and pass it as a callback function parameter. The available types are as follows:
xml: Returns an XML document that can be processed by JQuery.
html: Returns plain text HTML information; the included script tag will be executed when inserted into the DOM.
script: Returns plain text JavaScript code. Results are not automatically cached. Unless the cache parameter is set. Note that when making remote requests (not under the same domain), all post requests will be converted to get requests.
json: Returns JSON data.
jsonp: JSONP format. When calling a function in SONP form, such as myurl?callback=?, JQuery will automatically replace the last "?" with the correct function name to execute the callback function.
text: Returns a plain text string.


8.beforeSend:
The parameter is required to be a Function type. Before sending the request, you can modify the function of the XMLHttpRequest object, such as adding a custom HTTP header. If you return false in beforeSend, you can cancel this ajax request. The XMLHttpRequest object is the only parameter.
            function(XMLHttpRequest){
               this; //The options parameter passed when calling this ajax request
            }
9.complete:
Requires a parameter of type Function, the callback function to be called after the request is completed (called when the request succeeds or fails). Parameters: XMLHttpRequest object and a string describing the type of successful request.
          function(XMLHttpRequest, textStatus){
             this; //The options parameter passed when calling this ajax request
          }


10.success: The parameter required to be Function type, the callback function called after the request is successful, there are two parameters.
         (1) The data returned by the server and processed according to the dataType parameter.
         (2) A string describing the state.
         function(data, textStatus){
            //data may be xmlDoc, jsonObj, html, text, etc.
            this; //The options parameter passed when calling this ajax request
         }


11.error:
The parameter is required to be a Function type, when the request fails the function being called. This function has 3 parameters, namely XMLHttpRequest object, error message, captured error object (optional). The ajax event function is as follows:
       function(XMLHttpRequest, textStatus, errorThrown){
          //Usually only one of textStatus and errorThrown contains information
          this; //The options parameter passed when calling this ajax request
       }


12.contentType:
The parameter is required to be String type. When sending information to the server, the content encoding type defaults to "application/x-www-form-urlencoded". This default value is suitable for most applications.


13.dataFilter:
A function that requires a parameter of Function type to preprocess the original data returned by Ajax. Provide two parameters data and type. data is the original data returned by Ajax, and type is the dataType parameter provided when calling jQuery.ajax. 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 parameter of Function type to preprocess the original data returned by Ajax. Provide two parameters data and type. data is the original data returned by Ajax, and type is the dataType parameter provided when calling jQuery.ajax. The value returned by the function will be further processed by jQuery.
            function(data, type){
                //return the processed data
                return data;
            }


15.global:
Requires a parameter of type Boolean, the default is true. Indicates whether to trigger the global ajax event. Set to false will not trigger global ajax events, ajaxStart or ajaxStop can be used to control various ajax events.


16.ifModified:
Requires a parameter of type Boolean, the default is false. Only get new data when server data changes. The server data change judgment is based on the Last-Modified header information. The default value is false, which ignores the header information.


17.jsonp:
requires a parameter of type String, rewrite the name of the callback function in a jsonp request. This value is used in place of the "callback" part of the URL parameter in a GET or POST request such as "callback=?", e.g. {jsonp:'onJsonPLoad'} will cause "onJsonPLoad=?" to be passed to the server.


18.username:
The parameter required to be of type String is the username used to respond to the HTTP access authentication request.


19.password:
A parameter of type String required to respond to the password of the HTTP access authentication request.


20. processData:
The parameter is required to be a Boolean type, the default is true. By default, sent data will be converted to an object (technically not a string) 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 don't want to convert.


21.scriptCharset:
The parameter required to be of type String will only be used to force the modification of the charset when the dataType is "jsonp" or "script" and the type is GET. Typically used when the local and remote content encodings are different.


Example 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(); / /Empty all content in resText
                         var html = ''; 
                         $.each(data, function(commentIndex,

                                         + ':</h6><p class="para"' + comment['content']
                                         + '</p></div>';
                         });
                         $('#resText').html(html);
                      }
         });
    });
});
 


22. By the way, the $.each() function:
The $.each() function is different from the each() method of the JQuery object, it is a global function and does not operate on the JQuery object, but It takes an array or object as the first parameter and a callback function as the second parameter. The callback function has two parameters: the first is the index of the member or array of the object, and the second is the corresponding variable or content.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325780247&siteId=291194637