【问题标题】:jquery ajax error {"readyState":0,"responseText":"","status":0,"statusText":"error"}jquery ajax 错误 {"readyState":0,"responseText":"","status":0,"statusText":"error"}
【发布时间】:2011-10-24 11:57:48
【问题描述】:

我正在尝试做一个 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 调用(这显然不是)

我试过把完整的网址放进去,它做同样的事情。

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

【问题讨论】:

  • 你为什么不检查err? Firebug 告诉您有关请求和响应的哪些信息?
  • 当你将伪造的表单提交到具有相同数据的同一页面时,会发生什么?
  • err 是一个空白字符串,是的,一个假表单可以正常工作,但这个 ajax 仍然不起作用
  • 双“”呢? ,"responseText":"",
  • 这真的很奇怪,我认为这只是该脚本所在文件夹中的某些内容,因为我将完全相同的代码复制到了不同文件夹中的页面上,它工作正常吗?谁有线索?!

标签: jquery ajax


【解决方案1】:

就我而言,我遇到了错误

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

因为我的 php.ini 变量是

post_max_size = 8M
upload_max_filesize = 2M

我发布的文件是 43MB。 所以我将 php.ini 变量更改为

post_max_size = 300M
upload_max_filesize = 250M

【讨论】:

    【解决方案2】:

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

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

    Network: *

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

    【讨论】:

      【解决方案3】:

      我遇到了同样的错误:

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

      在我的情况下,一个 Firefox 浏览器运行良好,而另一个具有相同版本的 Firefox 浏览器给了我这个错误,甚至没有进行 ajax 调用。

      解决方案是在我的浏览器中禁用 Adblocker 插件。

      【讨论】:

        【解决方案4】:

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

        问题实际上是,如果你不停止弹出,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();
                    };
                }
            }
        });
        

        【讨论】:

          【解决方案5】:

          调用这个的元素是一个锚标记,它会调用这个然后下载一个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>
          

          【讨论】:

            猜你喜欢
            • 2013-10-24
            • 2014-12-17
            • 2018-12-24
            • 2016-05-20
            • 1970-01-01
            • 2020-11-26
            • 2012-12-30
            • 2014-09-21
            • 1970-01-01
            相关资源
            最近更新 更多