我正在尝试使用dataType: "json"
进行AJAX请求,我的Firebug一直显示请求为"中止"。"
POST http://mydomain.com/path/to/page Aborted
这是代码片段:
var postData = "someVar=someValue&otherVar=otherValue", msg;
$.ajax({
type: "POST",
url: "/path/to/page",
data: postData,
dataType: "json",
cache: false,
success: function(response) {
if (typeof response.row != undefined) {
$('#my-select')
.append($('<option></option>')
.attr("value", response.row.id)
.text(response.row.name)
);
msg = response.msg;
} else {
msg = 'failed';
}
alert(msg);
},
error: function(xhr, status, thrown) {
// EDIT 1
alert(status); // <-- It's alerting "timeout"
}
});
顺便说一句,我在这个网站上看到了一堆有类似问题的人的问题,但其中可能是非常具体的。一个是因为请求太大,另一个是因为他们请求到另一个域,等等。
我得到没有响应头和没有响应体。
这是我的请求头(简化):
Host mydomain.com
User-Agent Firefox/8.0.1
Accept application/json, text/javascript, */*; q=0.01
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip, deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
DNT 1
Connection keep-alive
Content-Type application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With XMLHttpRequest
Referer http://mydomain.com/path/to/page
Content-Length 125
Cookie PHPSESSID=somerandomstring
Apache error_log中没有显示任何内容。我在浏览器中手动导航到那个页面,没有出现任何错误(只有一个白屏和一些文本)。
注意:这适用于我的本地主机,但不适用于测试服务器。
编辑:添加error()
回调,并警告"超时"。"
请求实际上可能超时
首先要尝试的是将默认超时值增加一点,像这样:$.ajax({
type: "POST",
url: "/path/to/page",
data: postData,
timeout: 3000,
dataType: "json",
[...]
默认超时值显然取决于浏览器,因此它可能低于脚本执行时间,或者由其他东西设置,等等。
JQuery ajax调用默认超时值