The way Ajax uses error debugging errors in jquery _jquery

Source: Internet
Author: User

This example describes the way Ajax uses error debugging errors in jquery. Share to everyone for your reference. The specific analysis is as follows:

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.

The common usage of Ajax in jquery is similar to:

$ (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
 }

The error function is invoked when an error occurs asynchronously through an Ajax call. The error function syntax is:

(Default: Automatic judgment (XML or HTML)) when a request fails to call time.
//parameter has the following three: XMLHttpRequest object, error message, (optional) caught Error object.
//If an error occurs, the error message (the second parameter), in addition to being NULL,
//may also be "timeout", "error", "Notmodified", and "ParserError".
 
//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 ');

I hope this article will help you with your jquery programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.