javascript Jquery ajaxrequest xhr.status 代码 0 但 html 状态代码 200

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8832359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 04:43:26  来源:igfitidea点击:

Jquery ajaxrequest xhr.status code 0 but html status code 200

javascriptjqueryajaxhttp-status-code-200

提问by mtheitroadan

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

这不是跨站点请求问题,因为我的问题的大多数 google 搜索答案都包括。

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.

尝试使用 jquery 函数 .get 和 .load 发出 ajax 请求时,我收到 xhr.status 0 och xhr.statusText“错误”。但是在 Firebug 中,请求的页面加载了 html 状态代码 200,我可以读取响应中的文本并且响应标头看起来没问题。

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

Server: IIS v.7.5 Jquery: v1.6.2

服务器:IIS v.7.5 Jquery:v1.6.2

Any ideas?

有任何想法吗?

回答by loesak

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.

我相信零状态代码可能意味着很多事情。就我而言,由于用户在请求-响应周期完成之前离开页面,我收到的状态代码为零。如果与服务器的通信失败,您还可以获得零状态代码。我基本上将此总结为发件人出站通信失败。这就是为什么它还会提出跨站点请求和其他与客户端打交道的原因。

The W3 XMLHttpRequest specification section 4.8.1states:

W3 XMLHttpRequest 规范第 4.8.1 节指出:

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.

status 属性必须返回运行这些步骤的结果:
如果状态为 UNSENT 或 OPENED,则返回 0 并终止这些步骤。
如果设置了错误标志,则返回 0 并终止这些步骤。
返回 HTTP 状态代码。

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:

如果我正确阅读了规范,则 UNSENT 和 OPENED 是请求在发送之前可以处于的状态。如果出现以下情况,则会设置错误标志:

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

错误标志表示某种类型的网络错误或请求中止...

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.

我对此状态代码的问题是,如果用户从导航中中止请求,我不认为这是错误情况,因为这是用户的故意操作。网络问题 我确实认为是错误。例如,如果用户调用导致 ajax 请求的操作,该请求有一个错误处理程序将他们重定向到错误页面。如果用户要离开当前页面,从而中止请求,从可用性的角度来看,将他们重定向到错误页面是没有意义的。规范的实现很好,只是我还没有确定一种方法来知道零状态代码来自中止的请求或其他原因。如果我能够确定差异,我将能够采取适当的行动。

EDIT: 03/18/2014

编辑:03/18/2014

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.

为了回应@ariera 在 2013 年 8 月 9 日的评论,我提供了对解决方法的粗略描述。然而,问题是我已经有一段时间没有解决这个问题了,我的记忆很模糊。

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.

如果我没记错的话,确定状态代码 0 是否是由于用户间接中止请求引起的想法是将错误处理代码放在与错误回调分开的自己的回调中。然后在错误回调中,检查状态代码是否为零。如果是,请在超时延迟内调用错误回调,否则调用错误回调。这里的想法是,如果错误代码为零,则在处理该错误时延迟允许浏览器实际执行导航。允许浏览器执行导航应该清除任何正在执行的 JavaScript,因此错误处理程序不会触发。

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.

现在这是一个混乱,甚至可能不再起作用,我真的不记得问题的复杂性,但希望它可以引导其他人走上正确的道路。

回答by WTK

Response status code 0, was caused (in my case while I was developing a website) by making xhr requests from page served by my local server to the same server on localhost.

响应状态代码 0 是通过从本地服务器提供的页面向本地主机上的同一服务器发出 xhr 请求引起的(在我的情况下是在我开发网站时)。

Other reason (with possible solutions) can be found here:

其他原因(可能的解决方案)可以在这里找到:

"Make sure that you are using relative URLs for your AJAX requests." - invalid HTTP response code ‘0'

“确保您对 AJAX 请求使用相对 URL。” - 无效的 HTTP 响应代码“0”