jquery encapsulates Ajax as a function, which we use very conveniently, jquery automatically submits the data based on the Content selection post or get, and it automatically encodes it, but to fully grasp the Ajax in jquery, we have to remember its various parameters:
1, URL strng send the requested address
2, type string request mode (post or get, default get), other HTTP request methods, such as: Put and delete can also be used, but only some browser support.
3. Timeout number sets the request time-out (in milliseconds). This setting overrides the global settings.
4, Async Bollean Whether the asynchronous request, by default is true.
5, Beforesend function can modify the functions of the XMLHttpRequest object before sending the request, such as custom HTTP request header, in Beforesend if return false can cancel this Ajax request. The XMLHttpRequest object is the only parameter.
function (XMLHttpRequest) {
this;//The parameter option passed when calling this Ajax request;
}
6. The callback function after complete function request is completed, whether the request succeeds or the failure is called; its argument is a XMLHttpRequest object-level success message string.
function (xmlhttprequest,textstatus) {
this;//The parameter option passed when calling this Ajax request;
}
7, ContentType string default is: ("application/x-www-form-urlencoded"), to send information to the server when the content encoding type. The default values are suitable for most applications.
8. Data object,string is passed to the server and will be automatically converted to the request string format. The GET request appends it to the URL, viewing the ProcessData option description to disallow this automatic conversion. You must format the key-value pair, and if it is an array, jquery automatically corresponds to the same name for the different values. such as {foo:["bar1", Bar2]} converted to "&foo=bar1&foo=bar2";
9. DataType string is expected to process the data type returned by the server. If not specified, JQ automatically returns Responsexml or Resonsetext based on the HTTP packet mime information, and is passed as a callback function parameter, with the available values:
"XML": Returns an XML document that can be processed with JQ.
"HTML": Returns HTML information for plain text, including script;
"Script": Returns plain text JavaScript code, does not automatically cache the results;
"JSON": returns JSON data;
"JSONP": Jsonp format, using JSONP form electrophoresis function, such as "myurl?callback=?"; JQ will automatically replace it with the correct function name to execute the callback function.
10. This method is called when the error function request fails. This method has three parameters:
XMLHttpRequest
Object
Error message, (possibly) the error object being captured;
function (Xmlhttprequest,textstatus,errorthrown) {
Typically, Textstatus and Errorthrown
this;//The parameter option passed when calling this Ajax request; only one of them has a value;
}
11, global Bollean whether to trigger an overall Ajax event, by default True, set to false will not trigger global AJAX events, such as Ajaxstart or Ajaxstop. Can be used to control different AJAX events.
12. Ifmodified Bollean only gets new data when the server data changes (default false). According to the HTTP packet last-modified header information to judge.
13, ProcessData Bollean By default, the data sent will be converted to an object (technically not a string) with the default content type: "application/x-www-form-urlencoded". Set to False if you want to send DOM number information or other information that you do not want to convert.
14. Success function requests a successful even callback. This method has two parameters: the server returns the data and returns the status:
function (data,textstatus) {
Data could be xmldoc, jsonobj, HTML, text, etc.
This The options for this AJAX request
}
The 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 (); Empty everything inside the restext.
var html = ';
$.each (data, function (Commentindex, comment) {
HTML + = ' <div class= "comment" >
+ ':
+ ' </p></div> ';
});
$ (' #resText '). HTML (HTML);
}
});
});
});
JQ in the Ajax is the interviewer likes to ask questions, but also the actual work will often encounter, need to remember firmly!
Ajax parameters in jquery