The meaning of each parameter of $.ajax

AJAX = Asynchronous JavaScript and XML.

AJAX is not a new programming language, but a new way to use existing standards.

AJAX is the art of exchanging data with a server and updating parts of a web page without reloading the entire page.

        $.ajax({
                type:"post",
                url:HYDname+'/api/SSO/ValidCode',
                data: doCode,
                async:false,
                contentType:'application/json;chartset=UTF-8',
                success:function(data){
                    alert(data)
                },
                error:function(err){
                    alert(err)
                }
            });

Common parameters are:

1、url

    Requires a parameter of type String, (defaults to the current page address) the address to send the request to.

    This is where the request is sent to the corresponding background for processing, and the background distinguishes different requests according to this url.

2、type

    The request is a parameter of type String, and 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.

    post: The browser sends each form field element and its data to the Web server as the entity content of the HTTP message. The amount of data is much larger than that transmitted by GET, which is safe.

    get: The get method can transmit simple data, there is a size limit, the data is appended to the url and sent (HTTP header transmission), the url can be cached by the client, and the client data can be obtained from the browser's history, which is not safe.

3、async

    The request is a parameter of type Boolean. The default setting is true. 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.

    There will often be problems here, that is, asynchronous , so sometimes you will be careless. Before the request is successful, you think you are successful, and then do other things, it is easy to have problems, so generally, you need to call back in success Do something inside the function.

4、data

    The data sent to the server requires a parameter of type Object or String. 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, see the processData (prevent automatic conversion) 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"]} translates to &foo=bar1&foo=bar2.

5、datatype

    Requires a parameter of type String , expected data type returned by the server . 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, which 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. At least I have seen all return json type. Others have not been used. The background can return a bean object after processing, and then convert the object into an object in the form of a json string, just like the stream object in the top example, you can easily operate various properties, and then operate in the foreground. Very convenient. . . In one sentence: If it is specified as json type, the obtained data will be parsed as a JavaScript object, and the constructed object will be returned as the result.

    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.

6、success

    The request is a parameter of type Function , and 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          }





7、error

    Requires a parameter of type Function, the function to be called when the request fails. 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
       }

8、contentType

    Requires a parameter of type String. 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.

    multipart/form-data: Sometimes this is also used for uploading and downloading.

    contentType: "application/json; charset=utf-8"

Other less commonly used

1、timeout

    The request is a parameter of type Number, and sets the request timeout time (milliseconds). This setting will override the global setting of the $.ajaxSetup() method.

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

3、beforeSend

    This parameter is mainly used to perform some operations before sending a request to the server. 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
            }

4、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
          }

5 、 data filter

    A function that requires a parameter of type Function to preprocess the raw 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;
            }

6、global

    Requires an argument of type Boolean, defaults to 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.

7、ifModified

    Requires a parameter of type Boolean, defaults to 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.

8 、 jsonp

    Requires a parameter of type String to override 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.

9、username

    Requires a parameter of type String, which is the username used to respond to HTTP access authentication requests.

10、password

    Requires a parameter of type String, the password used to respond to HTTP access authentication requests.

11、processData

    Requires an argument of type Boolean, defaults to 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.

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

 

    

    

Guess you like

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