2011-08-10 96 views
12

我试图做一个Ajax请求jQuery的Ajax错误{ “readyState的” 0 “responseText的”: “”, “状态”:0, “状态文本”: “错误”}

$.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调用(这显然不是)

我已经尝试把完整的网址,它也做同样的事情。

我能想到的唯一的事情就是标题,我不知道会出现什么问题。下面是从萤火虫

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 
+0

你为什么不检查'err'? Firebug告诉你关于请求和响应的内容? –

+0

当您使用相同的数据将假表单提交到同一页面时,会发生什么情况? –

+0

err是一个空白字符串,是一个假表单工作得很好,但是这个ajax仍然不起作用 –

回答

4

我遇到了同样的问题来了:怎么每次注册一个用户点击链接。

问题是,如果你不停止弹出,ajax请求没有完成,你准备好了状态: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(); 
      }; 
     } 
    } 
}); 
1

元素那是在调用这个是一个锚标签,它会调用它,然后下载一个exe文件,当文件下载对话框强制它会取消这个请求,就好像它正在导航到一个新页面一样。

我改成了

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> 
0

我得到了同样的错误:

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

在我的情况下,一个火狐浏览器工作完美,另一个相同版本的Firefox浏览器给我这个错误,甚至没有进行ajax调用。

解决方案是在我的浏览器中禁用Adblocker加载项。

0

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

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

Network: *

这被加载防止请求的页面,没有任何有意义的错误消息。 即使这可能不是大多数情况下ajax失败的原因,但我希望至少可以挽救一些犯过同样错误的人。

相关问题