Detailed explanation of the $.ajax() method

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

 

1. url
Requires a parameter of type String, (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, 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.

3.timeout
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
The data sent to the server is required as a parameter of Object or String type. 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"]} translates to &foo=bar1&foo=bar2.

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 :
The parameter required to be a Function type, the callback function 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:要求为Function类型的参数,请求成功后调用的回调函数,有两个参数。
         (1)由服务器返回,并根据dataType参数进行处理后的数据。
         (2)描述状态的字符串。
         function(data, textStatus){
            //data可能是xmlDoc、jsonObj、html、text等等
            this;  //调用本次ajax请求时传递的options参数
         }

11.error:
要求为Function类型的参数,请求失败时被调用的函数。该函数有3个参数,即XMLHttpRequest对象、错误信息、捕获的错误对象(可选)。ajax事件函数如下:
       function(XMLHttpRequest, textStatus, errorThrown){
          //通常情况下textStatus和errorThrown只有其中一个包含信息
          this;   //调用本次ajax请求时传递的options参数
       }

12.contentType
要求为String类型的参数,当发送信息至服务器时,内容编码类型默认为"application/x-www-form-urlencoded"。该默认值适合大多数应用场合。

13.dataFilter
要求为Function类型的参数,给Ajax返回的原始数据进行预处理的函数。提供data和type两个参数。data是Ajax返回的原始数据,type是调用jQuery.ajax时提供的dataType参数。函数返回的值将由jQuery进一步处理。
            function(data, type){
                //返回处理后的数据
                return data;
            }

14.dataFilter
要求为Function类型的参数,给Ajax返回的原始数据进行预处理的函数。提供data和type两个参数。data是Ajax返回的原始数据,type是调用jQuery.ajax时提供的dataType参数。函数返回的值将由jQuery进一步处理。
            function(data, type){
                //返回处理后的数据
                return data;
            }

15.global
要求为Boolean类型的参数,默认为true。表示是否触发全局ajax事件。设置为false将不会触发全局ajax事件,ajaxStart或ajaxStop可用于控制各种ajax事件。

16.ifModified
要求为Boolean类型的参数,默认为false。仅在服务器数据改变时获取新数据。服务器数据改变判断的依据是Last-Modified头信息。默认值是false,即忽略头信息。

17.jsonp
要求为String类型的参数,在一个jsonp请求中重写回调函数的名字。该值用来替代在"callback=?"这种GET或POST请求中URL参数里的"callback"部分,例如{jsonp:'onJsonPLoad'}会导致将"onJsonPLoad=?"传给服务器。

18.username
要求为String类型的参数,用于响应HTTP访问认证请求的用户名。

19.password
要求为String类型的参数,用于响应HTTP访问认证请求的密码。

20.processData
要求为Boolean类型的参数,默认为true。默认情况下,发送的数据将被转换为对象(从技术角度来讲并非字符串)以配合默认内容类型"application/x-www-form-urlencoded"。如果要发送DOM树信息或者其他不希望转换的信息,请设置为false。

21.scriptCharset
要求为String类型的参数,只有当请求时dataType为"jsonp"或者"script",并且type是GET时才会用于强制修改字符集(charset)。通常在本地和远程的内容编码不同时使用。

案例代码:

复制代码
复制代码
$(function(){
    $('#send').click(function(){
         $.ajax({
             type: "GET",
             url: "test.json", data: {username:$("#username").val(), content:$("#content").val()}, dataType: "json", success: function(data){ $('#resText').empty(); //清空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); } }); }); });
复制代码
复制代码

 

22.顺便说一下$.each()函数:
$.each()函数不同于JQuery对象的each()方法,它是一个全局函数,不操作JQuery对象,而是以一个数组或者对象作为第1个参数,以一个回调函数作为第2个参数。回调函数拥有两个参数:第1个为对象的成员或数组的索引,第2个为对应变量或内容。

Guess you like

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