jQuery的Ajax错误{"readyState":0,"responseText":"","status":0,"statusText":"error"} [英] jquery ajax error {"readyState":0,"responseText":"","status":0,"statusText":"error"}

查看:2729
本文介绍了jQuery的Ajax错误{"readyState":0,"responseText":"","status":0,"statusText":"error"}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个Ajax请求

  $。阿贾克斯({
  类型:后,
  网址:的download.php
  错误:功能(数据,状态,ERR){
           警报(JSON.stringify(数据));
         },
  数据:FILEID =+ FILEID
});
 

此请求警报{readyState的0的responseText:,状态:0,状态文本:错误}

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

我试图把完整的URL中,它做同样的事情。

我唯一能想到的是头,我不知道什么是错的。下面是从萤火请求头

 主机www.mydomain.com
用户代理的Mozilla / 5.0(Windows NT的6.1; RV:5.0)的Gecko / 20100101火狐/ 5.0
接受              */*
接受语言EN-US,EN; Q = 0.5
接受编码gzip压缩,紧缩
接收字符集ISO-8859-1,UTF-8,Q = 0.7,*; Q = 0.7
连接保持
内容类型application / x-WWW的形式urlen codeD;字符集= UTF-8
的X请求-随着XMLHtt prequest
Referer的http://www.mydomain.com/
内容长度8
曲奇PHPSESSID = 27b7d3890b82345a4fc9604808acd928
 

我添加了一个不同的页面上的其他要求,它工作得很好,但这个一直没有头的其他要求是:

 主机www.mydomain.com
用户代理的Mozilla / 5.0(Windows NT的6.1; RV:5.0)的Gecko / 20100101火狐/ 5.0
接受纯文本/,* / *; Q = 0.01
接受语言EN-US,EN; Q = 0.5
接受编码gzip压缩,紧缩
接收字符集ISO-8859-1,UTF-8,Q = 0.7,*; Q = 0.7
连接保持
内容类型application / x-WWW的形式urlen codeD;字符集= UTF-8
的X请求-随着XMLHtt prequest
Referer的http://www.mydomain.com/differentpage.php
内容长度33
曲奇PHPSESSID = 27b7d3890b82345a4fc9604808acd928
 

解决方案

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

现在的问题是逸岸,如果你不停止弹出,Ajax请求无法完成,你会得到的readyState:0

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

  / * ------------------------------------ --------------------------------------
 *该插件'下载'类在每一个页面的锚标记(链接)前
 *此脚本没有休息
 *
 *记得要改变'your_php_file你们用一
 * ------------------------------------------------- ------------------------- * /

$(文件)。就绪(函数()
{

    //检查是否有带班下载的任何链接
    如果(typeof运算$('下载')!=未定义)
    {
        VAR链接= $('下载');

        //运行此为每个下载链接
        对于(VAR I = 0; I< links.length;我++)
        {
            //设置点击行为
            链接[I] .onclick =功能(五)
            {
                //获取下载名称
                VAR ATTR = this.attributes,
                    HREF = attr.href.textContent,
                    ELEM = href.split('/'),
                    ELEM = ELEM [elem.length  -  1];

                //发送的下载文件名,只有在完成请求之后,让用户下载的文件
                $阿贾克斯(
                {
                    键入:POST,
                    数据类型:文本,
                    //'your_php_file'必须是ABSOLUT或相对路径!
                    网址:your_php_file,
                    //ELEM是包含下载名称的变量
                    //你可以通过$ _ POST ['download_name']调用它在你的PHP文件
                    数据:{download_name:ELEM},
                    //这里,我们去魔法:
                    请求完成//运行后弹出的下载
                    完成:函数()
                    {
                        window.location.href = HREF;
                    }
                });

                //停止默认的行为,直到Ajax请求进行完
                即preventDefault();
            };
        }
    }
});
 

I am trying to do an ajax request

$.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"}"

I have searched on google all i have come up with is a cross site ajax call(which this is obviously not)

I have tried putting the full url in and it does the same thing.

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

解决方案

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!

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

这篇关于jQuery的Ajax错误{&quot;readyState&quot;:0,&quot;responseText&quot;:&quot;&quot;,&quot;status&quot;:0,&quot;statusText&quot;:&quot;error&quot;}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆