有谁知道jquery ajax的请求出错指的是哪些情况吗?
也就是说 ajax error对应的方法在什么情况下会执行?
我写了一个jsp文件,里面有一个javascript方法,如下:
//单击一个按钮时执行
function checkboxclick(){
alert("enter");
$.ajax({
url: "PCustomerServlet",
type: "GET",
cache: false,
dataType: "xml",
error: function(datas){
alert(‘Error loading XML document’+datas);
},
success: function(datas){
alert(datas);
}
});
}
执行完alert方法后,随后就弹出第二个alert方法里面的内容。
在firefox的firedebug工具中可以看到服务器返回的xml文件
这里还有一个问题,就是请求URL有点怪:
http://localhost:8080/geely/customer/pcustomer/PCustomerServlet?_=1270288303338
不知道这个URL后面的参数是怎么来的。
有谁碰到过这样的问题没有啊?

window对象默认有name属性的,不声明的话 name是指 window.name换变量名,或者用var声明一下。HTML code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<script type="text/javascript">
function check()
{
var name=document.getElementById("t1");
var password=document.getElementById("t2");
alert(name.value);
alert(password.value);
}
</script>
<body>
1<input type="text" id="t1" value="1" /><br />
2<input type="text" id="t2" value="2"/>
<input type="button" value="登录" onclick="check()" />
</body>
</html>
不好意思,贴错贴了
因为设置了cache:false,为了防止IE缓存之前访问过的URL的数据,所以在url中加一个时间戳就变成了PCustomerServlet?_=1270288303338 ,一般不影响Servlet的执行的上述代码 ff3.6下测试没有抱error的alert
我在ff3.6下测试还是出现了同样的问题。找了好多资料,还是不知道jquery中ajax的error方法是在什么情况下执行的?急啊!!!!!!!!!!!!!!!
看jquery的源文件就知道了,是由handleError函数触发的,而handleError函数有2处调用,一是xhr发送xhr.send()异常, 二是返回的状态不是"success"。 不是"success"的状况除了xhr返回状态不是2XX 304之外。还包括 返回的数据的分析异常,其中与返回XML相关代码:JScript code
var ct = xhr.getResponseHeader("content-type"),
xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0,
data = xml ? xhr.responseXML : xhr.responseText;
if ( xml && data.documentElement.tagName == "parsererror" )
throw "parsererror";
关键代码:JScript code
if ( status == "success" ) {
// Watch for, and catch, XML document parse errors
try {
// process the data (runs the xml through httpData regardless of callback)
data = jQuery.httpData( xhr, s.dataType, s.dataFilter );
} catch(e) {
status = "parsererror";
}
}
if ( status == "success" ) {
…
} else
jQuery.handleError(s, xhr, status);
handleError: function( s, xhr, status, e ) {
// If a local callback was specified, fire it
if ( s.error ) s.error( xhr, status, e );
…
},