Home > Web Front-end > JS Tutorial > How to use error to debug errors in ajax in jquery_jquery

How to use error to debug errors in ajax in jquery_jquery

WBOY
Release: 2016-05-16 16:15:14
Original
878 people have browsed it

The example in this article describes how to use error to debug errors in ajax in jquery. Share it with everyone for your reference. The specific analysis is as follows:

JQuery allows us to improve efficiency and reduce many compatibility issues when developing Ajax applications. In our Ajax project, what should we do if we encounter an error in ajax asynchronously obtaining data? We can capture the error event to obtain the error. information.

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; // 调用本次AJAX请求时传递的options参数
   }
 });
  });
});

Copy after login

When the asynchronous call through ajax is successful, the success function will be called. The success function syntax is:

 //请求成功后回调函数。这个方法有两个参数:服务器返回数据,返回状态
 function (data, textStatus)
 {
  // data could be xmlDoc, jsonObj, html, text, etc...   
  this;
 // the options for this ajax request
 }

Copy after login

When an error occurs during asynchronous ajax call, the error function will be called. The error function syntax is:

//(默 认: 自动判断 (xml 或 html)) 请求失败时调用时间。
//参数有以下三个:XMLHttpRequest 对象、错误信息、(可选)捕获的错误对象。
//如果发生了错误,错误信息(第二个参数)除了得到null之外,
//还可能是"timeout", "error", "notmodified" 和 "parsererror"。
 
//textStatus: "timeout", "error", "notmodified" 和 "parsererror"。

error:function (XMLHttpRequest, textStatus, errorThrown) 
{ 
 
} 

Copy after login

The first parameter XMLHttpRequest returned by the error event:
XMLHttpRequest.readyState: The meaning of status code
0 - (uninitialized) send() method has not been called yet
1 - (Loading) The send() method has been called and the request is being sent
2 - (Loading completed) The send() method has been executed and all response content has been received
3 - (Interactive) Parsing response content
4 - (Complete) The response content parsing is completed and can be called on the client

Sending errors may be caused by the following two, or other procedural problems, which require us to be careful.
1. data: "{}", if the data is empty, you must pass "{}"; otherwise, the returned data will be in xml format. And prompt parsererror.
2. The exception of parsererror is also related to the Header type. and encoding header('Content-type: text/html; charset=utf8');

I hope this article will be helpful to everyone’s jQuery programming.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template