jquery ajax error {“readyState”:0,“responseText”:“”,“status”:0,“statusText”:“error”}

我正在尝试做一个ajax请求

$.ajax({ type: "post", url: "download.php", error: function(data, status, err){ alert(JSON.stringify(data)); }, data: "fileid="+fileid }); 

此请求提醒“{”readyState“:0,”responseText“:”“,”status“:0,”statusText“:”error“}”

我在谷歌搜索了所有我想出的是一个跨站点ajax调用(这显然不是)

我已经尝试将完整的URL放入其中并执行相同的操作。

我唯一能想到的是标题,我不知道它会出现什么问题。 这是来自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 

我在另一个页面上添加了另一个请求并且它工作得很好但是这一个仍然使另一个请求的标题失败是:

 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 

我遇到了同样的问题:每次用户点击链接时如何注册。

问题是,如果你不停止弹出,ajax请求没有完成,你得到readyState:0!

我已经做了上面的另一个版本,可能更具可读性(即使更详细)

 /* -------------------------------------------------------------------------- * 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(); }; } } }); 

调用它的元素是一个锚标记,它会调用它然后下载一个exe文件,当文件下载对话框加速时,它会取消这个请求,好像它正在导航到一个新页面。

我改成了

 function download(fileid, href) { $.post("download.php", {task: "download", fileid: fileid}, function(data){ window.location.href = href; }); } Download 

我得到了同样的错误:

 "readyState: 0" "responseText: undefined" "status: 0" "text status: error" "error: " 

在我的情况下,一个Firefox浏览器工作正常,另一个版本相同的Firefox浏览器在没有进行ajax调用的情况下给了我这个错误。

解决方案是在我的浏览器中禁用Adblocker Add-On。

我浪费了3个小时的时间,直到我发现导致ajax失败的原因是在我的情况下使用readyState:0。

问题是由于使用了应用程序缓存。 在清单文件中,我失踪了

Network: *

这会阻止加载请求的页面,而不会出现任何有意义的错误消息。 虽然这可能不是大多数情况下ajax失败的原因,但我希望它至少可以挽救一些犯同样错误的人。