jquery allows us to improve efficiency when developing AJAX applications and reduce many compatibility issues, and we can get the wrong information by capturing the error event in an AJAX project when we encounter an error in the AJAX asynchronous fetch data.
To share a detailed list of Ajax parameters in jquery before you introduce the text to you:
| Name of parameter |
Type |
Describe |
| Url |
String |
(Default: Current page address) sends the requested address. |
| Type |
String |
(Default: "Get") Request method ("POST" or "get"), default to "get". Note: Other HTTP request methods, such as put and DELETE, are also available, but only partially supported by browsers. |
| Timeout |
Number |
Sets the request timeout (in milliseconds). This setting overrides the global setting. |
| Async |
Boolean |
(Default: TRUE) The default setting is that all requests are asynchronous requests. If you need to send a sync request, set this option to false. Note that the synchronization request will lock the browser and the user's other actions must wait for the request to complete before it can be executed. |
| Beforesend |
Function |
You can modify the functions of the XMLHttpRequest object before sending the request, such as adding a custom HTTP header. The XMLHttpRequest object is the only parameter.
function (XMLHttpRequest) { This //The options for this AJAX request } |
| Cache |
Boolean |
(default: TRUE) JQuery 1.2 new functionality, set to false will not load request information from the browser cache. |
| Complete |
Function |
The callback function (called when the request succeeds or fails) after the request completes. Parameters: XMLHttpRequest Object, Success message string.
function (XMLHttpRequest, Textstatus) { This //The options for this AJAX request } |
| ContentType |
String |
(Default: "application/x-www-form-urlencoded") the content encoding type when sending information to the server. Default values are appropriate for most applications. |
| Data |
Object, String |
Data sent to the server. is automatically converted to the request string format. The GET request is appended to the URL. View the ProcessData option description to prevent this automatic conversion. Must be in the Key/value format. If you are an array, JQuery will automatically correspond to the same name for different values. such as {foo:["bar1", "Bar2"]} is converted to ' &foo=bar1&foo=bar2 '. |
| dataType |
string |
The type of data that is expected to be returned by the server. If not specified, jquery will automatically return Responsexml or responsetext based on the HTTP packet MIME information and pass as a callback function argument, available values: XML: Returns an XML document that can be processed with jQuery. HTML: Returns plain text HTML information, including script elements. Script: Returns the plain text JavaScript code. Results are not automatically cached. "JSON": Returns JSON data. "Jsonp":   Jsonp   format. Use   JSONP when calling a function, such as "myurl?callback=?" JQuery is automatically replaced? To the correct function name to execute the callback function. |
| Error |
Function |
(Default: Automatic judgment (XML or HTML)) This method is called when a request fails. This method has three parameters: the XMLHttpRequest object, the error message, and (possibly) the error object being caught.
function (XMLHttpRequest, Textstatus, Errorthrown) {
This //The options for this AJAX request } |
| Global |
Boolean |
(default: TRUE) triggers a global AJAX event. Setting to FALSE will not trigger global AJAX events, such as Ajaxstart or Ajaxstop. Can be used to control different AJAX events |
| Ifmodified |
Boolean |
(default: false) gets new data only when the server data changes. Use HTTP packet last-modified header information to determine. |
| ProcessData |
Boolean |
(default: TRUE) by default, the data sent is 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 do not want to convert. |
| Success |
Function |
callback function after successful request. This method has two parameters: the server returns the data, returns the status
function (Data, Textstatus) { Data could be xmldoc, jsonobj, HTML, text, etc ... This //The options for this AJAX request } |
The common usage of Ajax in jquery is similar to:
JScript Code
$ (document). Ready (function () {
jQuery ("#clearCac"). Click (function () {
Jquery.ajax ({
url:url),
Type: "POST",
data: {ID: ' 0 '},
DataType: "JSON",
success:function (msg) {
alert (msg);
Error:function (XMLHttpRequest, Textstatus, Errorthrown) {
alert (xmlhttprequest.status);
alert (xmlhttprequest.readystate);
alert (textstatus);
},
complete:function (XMLHttpRequest, textstatus) {this
;// The options parameter to be passed when invoking this Ajax request}});
The success function is invoked when the asynchronous invocation succeeds through Ajax. The success function syntax is:
callback function after successful request. This method has two parameters: Server return data, return status
function (textstatus)
{
///data could be xmldoc, jsonobj, HTML, text, etc ... This
;
The options for this AJAX request
}
will invoke the error function when an error occurs asynchronously with an Ajax call. The error function syntax is:
//(Default: Automatic judgment (XML or HTML)) call time when a request fails. The parameter has the following three: XMLHttpRequest object, error message, optionally caught error object. If an error occurs, the error message (the second argument) may be "timeout", "error", "Notmodified", and "ParserError" in addition to being null.
//textstatus: "Timeout", "error", "Notmodified" and "ParserError".
error:function (XMLHttpRequest, Textstatus, Errorthrown)
{
The first argument returned by the error event XMLHttpRequest:
Xmlhttprequest.readystate: Meaning of the status code
0-(uninitialized) the Send () method has not been called
1-(load) called Send () method, sending request
2-(load complete) Send () method completed, received all response content
3-(interactive) parsing response content
4-(complete) The response content resolution is complete and can be invoked on the client
Send error may have the following two, or other procedural problems, we need to be careful.
1, data: "{}", data is empty also must pass "{}"; otherwise, the return is in XML format. and prompts ParserError.
2, ParserError of the exception and header type also related. and encoded header (' content-type:text/html; Charset=utf8 ');
The above content is a small series to share about jquery Ajax error debugging techniques, I hope you like.