jQuery的$就,错误处理程序不起作用不起作用、错误、程序、jQuery

2023-09-10 14:11:22 作者:好姑娘光芒万丈

您好,我注意到,这个简单的code不工作的方式,它应该工作...

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 =成功和数据= NULL。我甚至注意到,请求超时长之前10秒。 出现这种情况的Firefox(最新版本),铬(最新版本)和Safari 5,为什么呢?它是由于这一事实,我正在本地主机?

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?

我忘了告诉:请求不被缓存。其实这两种萤火虫和Chrome开发工具显示失败的请求。

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

大更新

此行​​为与使用本地主机。在如果我从另一个collegue 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的论坛上说,这是因为网络堆栈中止连接的方式,因为该主机是localhost。我测试了这个只能在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

推荐答案

更新时间::尝试更换测试(数据== NULL)(XMLHtt prequest.readyState === 4安培;&安培; XMLHtt prequest.status === 0)。

在约 XMLHtt prequest 标准的W3C候选推荐标准描述,它必须存在这样命名应使用错误标志表示某种类型的网络错误或流产。如果这类错误在状态 XMLHtt prequest 将是0,而不是200(OK),201(创建),304( 未修改)等。要准确对应http://www.w3.org/TR/XMLHtt$p$pquest/#the-status-attribute在 XMLHtt prequest.status 可以为0的情况下 XMLHtt prequest.readyState 等于0或1(UNSENT或开张)。另一种情况是 XMLHtt prequest.readyState = 4(DONE)和错误标志是真实的。如果我们没有来自这两种情况下的 XMLHtt prequest.status 必须是一个HTTP状态code。在http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html或http://www.w3.org/Protocols/HTTP/1.0/spec.html#Status-$c$cs没有HTTP状态code等于0,所以它似乎我的,你可以做以下

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