jquery ajax 错误 {"readyState":0,"responseText":"","status":0,"statusText":"error"}
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7018247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery ajax error {"readyState":0,"responseText":"","status":0,"statusText":"error"}
提问by Tony Brix
I am trying to do an ajax request
我正在尝试执行 ajax 请求
$.ajax({
type: "post",
url: "download.php",
error: function(data, status, err){
alert(JSON.stringify(data));
},
data: "fileid="+fileid
});
this request alerts "{"readyState":0,"responseText":"","status":0,"statusText":"error"}"
此请求提醒 "{"readyState":0,"responseText":"","status":0,"statusText":"error"}"
I have searched on google all i have come up with is a cross site ajax call(which this is obviously not)
我在谷歌上搜索过,我想出的是一个跨站点的ajax调用(这显然不是)
I have tried putting the full url in and it does the same thing.
我试过把完整的 url 放进去,它做同样的事情。
the only thing i can think of is the header and i don't know what would be wrong with it. Here is the request header from firebug
我唯一能想到的是标题,我不知道它会有什么问题。这是来自萤火虫的请求标头
Host www.mydomain.com
User-Agent Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.0
Accept */*
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
Connection keep-alive
Content-Type application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With XMLHttpRequest
Referer http://www.mydomain.com/
Content-Length 8
Cookie PHPSESSID=27b7d3890b82345a4fc9604808acd928
I have added another request on a different page and it works just fine but this one keeps failing the header for the other request is:
我在不同的页面上添加了另一个请求,它工作得很好,但是这个请求的标题一直失败于另一个请求是:
Host www.mydomain.com
User-Agent Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.0
Accept text/plain, */*; 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
Connection keep-alive
Content-Type application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With XMLHttpRequest
Referer http://www.mydomain.com/differentpage.php
Content-Length 33
Cookie PHPSESSID=27b7d3890b82345a4fc9604808acd928
采纳答案by a.barbieri
I came across the same issue: how to register every time a user click the link.
我遇到了同样的问题:每次用户点击链接时如何注册。
The problem is infact that if you don't stop the pop up, the ajax request doesn't complete and you get readyState: 0!
问题实际上是,如果你不停止弹出,ajax 请求就不会完成,你会得到 readyState: 0!
I've done another version of the above that maybe is more readable (even if more verbose)
我已经完成了上面的另一个版本,它可能更具可读性(即使更冗长)
/* --------------------------------------------------------------------------
* Before that add 'downloads' class to every anchor tag (link) in your page
* This script does the rest
*
* remember to change 'your_php_file' with the one you use
* -------------------------------------------------------------------------- */
$(document).ready( function()
{
// Check if there is any link with class 'downloads'
if ( typeof $('.downloads') != 'undefined' )
{
var links = $('.downloads');
// Run this for every download link
for ( var i = 0; i < links.length; i++ )
{
// Set click behaviour
links[i].onclick = function(e)
{
// Get download name
var attr = this.attributes,
href = attr.href.textContent,
elem = href.split('/'),
elem = elem[elem.length - 1];
// Send the download file name and only after completing the request let the user download the file
$.ajax(
{
type : "POST",
dataType : "text",
// 'your_php_file' must be an ABSOLUT or RELATIVE path!
url: your_php_file,
// 'elem' is a variable containing the download name
// you can call it in your php file through $_POST['download_name']
data: { download_name: elem },
// here we go magic:
// after the request is done run the popup for the download
complete: function()
{
window.location.href = href;
}
});
// Stop default behaviour until ajax request has been done
e.preventDefault();
};
}
}
});
回答by Tony Brix
The element that was calling this was an anchor tag that would call this then download an exe file and when the file download dialog box poped up it would cancel this request as if it were navigating to a new page.
调用它的元素是一个锚标记,它会调用它然后下载一个 exe 文件,当文件下载对话框弹出时,它会取消这个请求,就像导航到一个新页面一样。
I changed it to
我把它改成
function download(fileid, href)
{
$.post("download.php", {task: "download", fileid: fileid}, function(data){
window.location.href = href;
});
}
<a onclick="download(1, file.exe);return false;" href="file.exe">Download</a>
回答by Christian
I was getting the same error:
我遇到了同样的错误:
"readyState: 0"
"responseText: undefined"
"status: 0"
"text status: error"
"error: "
In my case, one Firefox Browser works perfectly and another Firefox Browser with the same version was giving me this error without even making the ajax call.
就我而言,一个 Firefox 浏览器运行良好,另一个具有相同版本的 Firefox 浏览器给了我这个错误,甚至没有进行 ajax 调用。
The solution was to disable an Adblocker Add-On in my browser.
解决方案是在我的浏览器中禁用 Adblocker Add-On。
回答by user2483352
I have just wasted 3 hours of my time until I found out what was causing ajax to fail with readyState: 0 in my case.
我刚刚浪费了 3 个小时的时间,直到我发现了导致 ajax 失败的原因, readyState: 0 在我的情况下。
The problem was due to the usage of application-cache. In the manifest-file I was missing
问题是由于使用了应用程序缓存。在清单文件中我丢失了
Network:
*
Network:
*
This prevented the requested page from being loaded, without any meaningful error message. Even though this may not be the reason for ajax to fail in most cases, I hope it will save at least some people who made the same mistake.
这阻止了请求的页面被加载,没有任何有意义的错误消息。尽管这可能不是大多数情况下 ajax 失败的原因,但我希望它至少可以挽救一些犯同样错误的人。