jQuery $.ajax,错误处理程序不起作用 [英] jQuery $.ajax, error handler doesn't work

查看:31
本文介绍了jQuery $.ajax,错误处理程序不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我注意到这个简单的代码无法正常工作...

Hello I noticed that this simple code doesn't work the way it is supposed to work...

    function test() {
    $.ajax( {
        'url' : 'test/GameConfiguration.json',
        'dataType' : 'json',
        data : {
            a : 'aaa'
        },
        cache : false,
        method : 'get',
        timeout : 10000, //10 secs of timeout 
        success : function(data, textStatus, XMLHttpRequest) {
            console.log("success");
            if (data == null)
                console.log("it's not a real success");
        },
        error : function(XMLHttpRequest, textStatus, errorThrown) {
            console.log("error: " + textStatus);
        }
    });
}

测试是在本地主机上运行的,我的意思是:我加载页面,关闭本地网络服务器,然后触发请求(通过一个简单的按钮,onclick 指向这个函数).错误永远不会被调用,我得到的是调用成功处理程序,它有 textStatus = "success" 和 data = null.我什至注意到请求在 10 秒之前就超时了.这发生在 Firefox(最新版本)、Chrome(最新版本)和 Safari 5 上.为什么会这样?是因为我在 localhost 上工作吗?

The test has been run on localhost, I mean: I load the page, I shut down the local webserver, then I fire the request (via a simple button with onclick pointing to this function). Error doesn't ever get called, what I get is to have the success handler called and it has textStatus = "success" and data = null. I even notice that the request times out long before 10 seconds. This happens on Firefox (last version), Chrome (last version) and Safari 5. Why this? Is it due to the fact I'm working on localhost?

我忘了说:请求没有被缓存.事实上,firebug 和 Chrome 开发工具都显示请求失败.

I forgot to tell: THE Request isn't cached. In fact both firebug and Chrome dev tools show the request to fail.

重大更新

此行为与使用 localhost 相关.事实上,如果我从另一台同事 PC 加载此页面,并且在触发请求之前,我将我的 PC 与网络断开连接,我会正确地将错误处理程序以超时作为状态触发.我认为这是jQuery的一个错误.这会让我很难测试超时错误:(

This behaviour is related to the use of localhost. In fact if I load this page from another collegue PC and before triggering the request I disconnect my PC from the network, I correctly get the error handler fired with timeout as status. I think this is a bug of jQuery. It will make me hard to test the timeout errors :(

来自 jQuery 论坛的人说这是由于网络堆栈中止连接的方式,假设主机是本地主机.我仅在 Windows 7 上对此进行了测试.如果您想在其他系统上对此进行测试,并且可以计算出一些 jQuery 内部结构,请在 jQuery 论坛上向此帖子报告:

Guys from jQuery forums say this is due to the way the network stack aborts the connection, given that the host is localhost. I tested this on Windows 7 only. If you feel like testing this on other systems and you can work out some jQuery internals, report to this post at the jQuery forums:

http://forum.jquery.com/topic/strange-and-unexpected-behaviour-of-ajax-error-and-localhost#14737000001331961

推荐答案

UPDATED: 尝试将测试 (data == null) 替换为 (XMLHttpRequest.readyState === 4 && XMLHttpRequest.status === 0).

UPDATED: Try to replace the test (data == null) to the (XMLHttpRequest.readyState === 4 && XMLHttpRequest.status === 0).

在关于XMLHttpRequest 标准的W3C Candidate Recommendation 中描述,它必须存在如此命名的错误标志",用于指示某种类型的网络错误或中止.如果出现此类错误,XMLHttpRequest 中的状态将为 0,而不是 200(OK")、201(Created")、304(Not Modified")等等.完全对应于 http://www.w3.org/TR/XMLHttpRequest/#the-status-attributeXMLHttpRequest.readyState 等于 0 或 1(UNSENT"或OPENED")的情况下,XMLHttpRequest.status 可以为 0").另一种情况是 XMLHttpRequest.readyState 等于 4(DONE")并且错误标志"为真.如果我们在这两种情况下都没有,则 XMLHttpRequest.status 必须是 HTTP 状态代码.在 http://www.w3.org/Protocols/rfc2616/rfc2616-sec10 下.htmlhttp://www.w3.org/Protocols/HTTP/1.0/spec.html#Status-Codes 没有等于 0 的 HTTP 状态代码.所以在我看来,您可以执行以下操作

In W3C Candidate Recommendation about XMLHttpRequest standard is described that it must exist so named "error flag" which should be used to indicates some type of network error or abortion. In case of such kind of errors the status in the XMLHttpRequest will be 0 instead of 200 ("OK"), 201 ("Created"), 304 ("Not Modified") and so on. To be exactly corresponds to http://www.w3.org/TR/XMLHttpRequest/#the-status-attribute the XMLHttpRequest.status can be 0 in case of XMLHttpRequest.readyState equal to 0 or 1 ("UNSENT" or "OPENED"). Another case is XMLHttpRequest.readyState equal to 4 ("DONE") and "error flag" is true. If we have no from these two cases the XMLHttpRequest.status must be a HTTP status code. Under http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html or http://www.w3.org/Protocols/HTTP/1.0/spec.html#Status-Codes there are no HTTP status code equal to 0. So it seems to my that you can do following

jQuery(document).ready(function () {
    $.ajax({
        type: 'GET',
        url:  'test/GameConfiguration.json',
        dataType: 'json',
        cache : false,
        success: function(data, textStatus, xhr){
            if (xhr.readyState === 4 && xhr.status === 0) {
                // if readyState is DONE (numeric value 4) then corresponds to
                // http://www.w3.org/TR/XMLHttpRequest/#dom-xmlhttprequest-done
                // "The DONE state has an associated error flag that indicates
                // some type of network error or abortion."
                // Corresponds to http://www.w3.org/TR/XMLHttpRequest/#the-status-attribute
                // If error flag it true, xhr.status is 0.
                alert('"error flag\" is true. It means that we have a network error"+
                      " or abortion (for example because of Same Origin Policy restrictions)');
            }
            else {
                alert(textStatus);
                alert(data);
            }
        },
        error: function(xhr, textStatus, errorThrown) {
            if (textStatus !== null) {
                alert("error: " + textStatus);
            } else if (errorThrown !== null) {
                alert("exception: " + errorThrown.message);
            }
            else {
                alert ("error");
            }
        }
    });
});

这篇关于jQuery $.ajax,错误处理程序不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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