我正在尝试使用 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"
}
});
顺便说一句,我在这个网站上看到了很多有类似问题的人的问题,但其中可能非常具体。一个是因为请求太大,另一个是因为他们正在请求另一个域,等等。
我没有响应 header 并且没有响应正文。
这是我的请求 header (已简化):
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",
[...]
显然,默认超时值取决于浏览器,因此它可能低于您的脚本执行时间,或者由其他设置等。
关于php - Firebug AJAX 请求中止 - 无响应正文或 header (同一域),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8539520/