jQuery的ajaxrequest xhr.status code 0,但HTML状态code 200 [英] Jquery ajaxrequest xhr.status code 0 but html status code 200

查看:807
本文介绍了jQuery的ajaxrequest xhr.status code 0,但HTML状态code 200的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是不是一个跨站点请求的问题,因为搜索频率最高的回答我的问题包括。

This is not a cross site request problem as most googled answers to my questions consists of.

我试图与jQuery的功能不用彷徨和.load一个Ajax请求时,得到xhr.status 0 OCH xhr.statusText错误。在萤火虫虽然,请求的页面加载HTML状态code 200,我可以读取响应文本和响应头看起来确定。

I'm getting xhr.status 0 och xhr.statusText "error" when trying to make an ajax request with jquery functions .get and .load. In Firebug though, the requested page is loaded with html status code 200, I can read the text in the response and the response header looks ok.

我的code:

  function GetProjectTask(e) {
    var loader = $('#' + e + 'tasks div.loader');
    var content = $('#' + e + 'tasks div.content');
    var img = $('#' + e + 'tasks img.collapseimg');
    if (loader.html() == null || loader.html().trim() == '') {
        if (content.html() == null || content.html() == '') {
            img.attr('src', 'images/expanded.gif');
            loader.html('<img src="images/loading.gif" alt="Wait..."> Loading support tasks');
            content.load('includes/my' + e + 'tasks.php',
                function (response, status, xhr) {
                    if (status == "error") {
                        var msg = "Sorry but there was an error: ";
                        alert(msg + xhr.status + " - " + xhr.statusText);
                    }
                }
            );
            return;
        }
    }
    content.empty();
    loader.empty();
    img.attr('src', 'images/collapsed.gif');
  } 

服务器:IIS v.7.5 jQuery的:V1.6.2

Server: IIS v.7.5 Jquery: v1.6.2

任何想法?

推荐答案

我相信状态$ C $零Ç可能意味着很多东西。在我来说,我收到了零的状态code由于用户离开页面前的请求 - 响应周期可以完成。你也可以得到零状态code。如果与服务器的通信失败。我已经基本总结此作为来自发送者出站通信的失败。这将是它为什么还配备了跨站点请求,并处理客户端的其他原因。

I believe status code of zero could mean many things. In my case I was receiving the status code of zero due to a user leaving the page before the request-response cycle could complete. You could also get a status code of zero if communication with the server failed. I have basically summarized this as a failure in out-bound communication from the sender. That would be why it also comes up with cross-site requests and other reasons dealing with the client side.

在W3 XMLHtt prequest规范第4.8.1节状态:

状态属性必须返回运行这些步骤的结果:
  如果状态未发送或打开,返回0,并终止这些步骤。
  如果错误标志设置,返回0,并终止这些步骤。
  返回的HTTP状态code。

The status attribute must return the result of running these steps:
If the state is UNSENT or OPENED, return 0 and terminate these steps.
If the error flag is set, return 0 and terminate these steps.
Return the HTTP status code.

如果我正确读取规格,UNSENT,打开要发送状态的请求可以是在之前。并且错误标志设置,如果:

If I read the specification correctly, UNSENT and OPENED are states the request can be in prior to being sent. And the error flag is set if:

错误标志表示某种类型的网络错误或要求堕胎......

The error flag indicates some type of network error or request abortion ...

这个问题我有这个状态code是如果一个请求是从航行距离中止用户,我不认为这是一个错误条件,因为它是由用户故意的行为。网络问题我考虑的一个错误。例如,如果用户调用其导致其中有一个错误处理程序,以将它们重定向到一个错误页面ajax请求的动作。如果用户要离开当前页面,因而中止请求,从可用性的角度来看,它是没有意义的,以它们重定向到错误页面。该规范的实施是好的,除了我还没有确定知道的零状态code来自中止请求或某些其他原因的方法。如果我能够确定的区别,我将能够采取适当的行动。

The problem I have with this status code is if a request is aborted by the user from navigating away, I do not consider this an error condition as it is a deliberate action by the user. Network issues I do consider an error. For example, if the user invokes an action that results in an ajax request which has an error handler to redirect them to an error page. If the user were to leave the current page, thus aborting the request, from a usability standpoint, it doesn't make sense to redirect them to the error page. The implementation of the specification is fine except for that I have yet to determine a method of knowing that a status code of zero came from an aborted request or for some other reason. If I were able to determine the difference, I would be able to take appropriate action.

修改:2014年3月18日

在响应由@ariera于2013年8月9日发表评论,我提供什么样的解决方法是粗略描述。然而,问题是,它已经有一段时间,因为我解决了这个和我的记忆是模糊的。

In response to comment by @ariera on Aug 9 2013, I'm supplying the rough description of what the workaround was. However, the problem is that it has been some time since i resolved this and my memory is vague.

如果我没有记错,这个想法围绕确定的0状态code是由于间接中止请求的用户是把错误处理code在自己的回调独立于错误回调。然后在您的错误回调,你检查,看看是否状态code是零。如果是,请致电超时延迟你的错误回调,否则叫你的错误回调。这里的想法是,如果错误code是零,那么将在该误差的处理的延迟允许浏览器实际执行导航。让浏览器进行导航应该清除任何执行的JavaScript,因此,错误处理程序将不会触发。

If i remember correctly, the idea around determining if the status code of 0 was due to the user indirectly aborting the request was to put the error handling code in its own callback separate from the error callback. Then in your error callback, you check to see if the status code is zero. If it is, call your error callback in a timeout delay, otherwise call your error callback. The idea here is that if the error code is zero, then putting a delay in the handling of that error allows the browser to actually perform the navigation. Allowing the browser to perform the navigation should clear any executing JavaScript and thus the error handler will not fire.

现在,这是一个杂牌,甚至可能不为再工作,我真的不记得了问题的复杂性,但希望它会导致其他人沿着正确的方向。

Now this is a kludge, and may not even work as again, I don't really remember the intricacies of the issues, but hopefully it leads someone else down the correct path.

这篇关于jQuery的ajaxrequest xhr.status code 0,但HTML状态code 200的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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