jQuery ajax由于未知原因而失败

时间:2011-03-31 20:20:39

标签: javascript jquery ajax windows-desktop-gadgets

我正在调用这样的函数:

function(activeOnly, callback) {
    debug("$.support.cors: {0}".format($.support.cors));
    debug("getting data({0})".format(activeOnly));
    debug("{0}/data/".format(this._api_root));
    $.ajax({
        url: "{0}/data/".format(this._api_root),
        data: {
            generaldata: !activeOnly
        },
        dataType: "json",
        headers: {
            authorization: this._auth_header
        },
        success: function (data, code, jqx) {
            debug("data request succeeded with {0}".format(data));
            result = data;
        },
        error: function(jqx, err, ex) {
            debug("data request failed with {0}: {1}".format(err, ex));                 
        },
        complete: function(jqx, status) {
            debug("data request completed: {0}".format(status));                    
        }
    });

作为响应,调用错误和完整函数,得到输出:

[6192] MyData: $.support.cors: true
[6192] MyData: getting data({0})
[6192] MyData: https://_some_root_/data/
[6192] MyData: data request failed with Unknown: Unknown
[6192] MyData: data request completed: Unknown

当我使用Charles进行监控时,这就是我得到的:

https://_some_root_/
Failed
No request was made. Possibly the certificate was rejected.
-
HTTP/1.0
CONNECT
-
/127.0.0.1
_some_root_/_some_ip_address_

3/31/11 3:15:28 PM
-
-
-
-
-
-
-
0.02 KB/s
-

366 bytes
-
-
-
366 bytes
-
-

在请求标签中,我可以看到我的请求看起来不正确(请注意缺少授权标头):

CONNECT _some_root_:443 HTTP/1.0
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3; Tablet PC 2.0)
Host: _some_root_:443
Content-Length: 0
Proxy-Connection: Keep-Alive
Pragma: no-cache

是什么给出的?没有提示无法识别的证书或类似的东西 - 它只是失败。 (如果我在浏览器中发出GET请求,它可以正常工作)。

1 个答案:

答案 0 :(得分:2)

这似乎是a known bug

  1. 根据记者的说法,解决方法/ hack是从

    更改jQuery源代码
    jQuery.support.cors = testXHR && ( "withCredentials" in testXHR );
    

    jQuery.support.cors = true; 
    
  2. 对该错误的评论有这样的说法:

      

    感谢错误报告,但这是   不是jQuery错误。 Windows 7小工具是   不是受支持的jQuery平台。该   运输新的ajax层是   可插拔的,所以你应该只包括   忽略正常的自定义传输   跨域支持(或使用   XDomainRequest)。

  3. ...和

      

    或者更简单地说,在脚本中将jQuery.support.cors设置为true而不侵入jQuery;)