jQuery ajax 因未知原因失败

发布于 2024-10-30 01:18:10 字数 1939 浏览 2 评论 0原文

我正在调用这样的函数:

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 请求,效果很好)。

I'm calling a function like this:

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

In response, the error and complete functions are called, with the resulting output:

[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

When I monitor using Charles, this is what I get:

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

and in the request tab, I can see that my request doesn't even look right (note the lack of an Authorization header):

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

What gives? There is no prompt for an unrecognized certificate or anything like that - it just fails. (It works fine if I make a GET request in my browser).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

始终不够 2024-11-06 01:18:10

这似乎是已知错误

  1. 根据记者的说法,解决方法/黑客是要改变jQuery 源代码来自

    jQuery.support.cors = testXHR && ( testXHR 中的“withCredentials”);
    

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

    <块引用>

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

  3. ...和

    <块引用>

    或者,更简单的是,在脚本中将 jQuery.support.cors 设置为 true,而无需侵入 jQuery ;)

This appears to be a known bug

  1. According to the reporter the workaround/hack is to change the jQuery source from

    jQuery.support.cors = testXHR && ( "withCredentials" in testXHR );
    

    to

    jQuery.support.cors = true; 
    
  2. A comment on the bug has this to say:

    Thanks for the bug report, but this is
    not a jQuery bug. Windows 7 Gadgets is
    not a supported jQuery platform. The
    transports layer of new ajax is
    pluggable, so you should just include
    a custom transport that ignores normal
    cross-domain support (or uses
    XDomainRequest).

  3. ...and

    Or, more simply, set jQuery.support.cors to true in your script without hacking into jQuery ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文