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

How to use error to debug errors in ajax in jquery

巴扎黑
Release: 2017-07-03 10:43:21
Original
1082 people have browsed it

This article mainly introduces the method of using error to debug errors in ajax in jquery. The example analyzes the usage of Ajax and the error functionThe techniques for debugging errors. Friends in need can refer to the following

The example of this article describes the method of using 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 use Capture errorevent to obtain 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 syntax of the error function 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) The 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 execution is completed, All response content has been received
3 - (Interaction) The response content is being parsed
4 - (Complete) The response content is parsed and can be called on the client

The error sent may include the following two Caused by, or other procedural issues, we need to be careful.
1. data: "{}", if the data is empty, you must pass "{}"; otherwise, the returned data will be in xml format. And prompts parsererror.
2. The exception of parsererror is also related to the Header type. And encoding header('Content-type: text/html; charset=utf8');

The above is the detailed content of How to use error to debug errors in ajax in jquery. For more information, please follow other related articles on the PHP Chinese website!

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