Home > Article > Web Front-end > What are the methods of ajax in jquery? Detailed explanation of ajax method in jquery
This article mainly introduces how to use ajax. There is also a detailed explanation of the ajax method in jQuery. Now let us read this article together
The meaning of the ajax method parameters in jquery is as follows:
1.url:
Requires a String type parameter, (default is the current page address) the address to which the request is sent.
2.type:
Requires parameters of String type, 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:
Requires a parameter of type Number and sets the request timeout (milliseconds). This setting will override the global setting of the $.ajaxSetup() method.
4.async:
Requires Boolean type parameters, the default setting is true, and all requests are asynchronous requests. If you need to send synchronous requests, set this option to false. 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:
Requires a Boolean type parameter, the default is true (when dataType is script, the default is false), setting it to false will not load the request information from the browser cache.
6.data:
Requires parameters of type Object or String, 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, check the processData option. The object must be in key/value format, for example {foo1:"bar1",foo2:"bar2"} is converted to &foo1=bar1&foo2=bar2. If it is an array, JQuery will automatically assign the same name to different values. For example, {foo:["bar1","bar2"]} is converted to &foo=bar1&foo=bar2.
7.dataType:
Requires parameters of String type, expected data type returned by the server. If not specified, JQuery will automatically return responseXML or responseText based on 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 with 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 cache parameters are set. Note that when making remote requests (not under the same domain), all post requests will be converted into get requests.
json: Return JSON data.
jsonp: JSONP format. When calling a function using the 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:
Requires parameters of Function type. You can modify the function of the XMLHttpRequest object before sending the request, such as adding a custom HTTP header. If false is returned in beforeSend, this ajax request can be canceled. The XMLHttpRequest object is the only parameter.
function(XMLHttpRequest){ this; //调用本次ajax请求时传递的options参数 }
9.complete:
Requires parameters of Function type, 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 successful request type.
function(XMLHttpRequest, textStatus){ this; //调用本次ajax请求时传递的options参数 }
10.success: The parameter is required to be 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可能是xmlDoc、jsonObj、html、text等等 this; //调用本次ajax请求时传递的options参数 }
11.error:
Requires a parameter of Function type, the function to be called when the request fails. This function has three parameters, namely XMLHttpRequest object, error message, and captured error object (optional). The ajax event function is as follows:
function(XMLHttpRequest, textStatus, errorThrown){ //通常情况下textStatus和errorThrown只有其中一个包含信息 this; //调用本次ajax请求时传递的options参数 }
12.contentType:
is required to be a String type parameter. 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. (If you want to see more, go to the PHP Chinese website AJAX Development Manual column to learn)
13.dataFilter:
is required to be a parameter of Function type, and is used to process the original data returned by Ajax Preprocessing functions. 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 data; }
14.dataFilter:
Requires parameters of Function type, a function that preprocesses 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 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 += '346aba7b39b1c0ee1f3b184fad76163a4e9ee319e0fa4abc21ff286eeb145ecc' + comment['username'] + ':489364b7e0629f9745997c79a31b7c6ddffa8ba8878e0ba38ca4846ff634519594b3e26ee717c64999d7867364b1b4a3'; }); $('#resText').html(html); } }); }); });
本篇文章到这就结束了(想看更多就到PHP中文网AJAX使用手册栏目中学习),有问题的可以在下方留言提问。
The above is the detailed content of What are the methods of ajax in jquery? Detailed explanation of ajax method in jquery. For more information, please follow other related articles on the PHP Chinese website!