English 中文(简体)
Jquery ajaxrequest xhr.status Code 0 but html status Code 200
原标题:Jquery ajaxrequest xhr.status code 0 but html status code 200

这不是一个交叉点要求的问题,因为对我的问题的大多数粗略的答复都包含在内。

I m 带xhr.status 0 och xhr.status 文本“error”在试图提出附带舱位功能的jax请求时。 然而,在“火焰”中,所要求的网页装上了《html地位法》200,我可以读到答复中的案文,回答者看着ok。

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

任何想法?

问题回答

我认为,“零”地位法典可能意味着许多事情。 在我的案件中,由于用户在请求答复周期完成之前离开网页,我收到了零状态代码。 如果与服务器的通信失败,你也可以获得零状态代码。 我基本上把这归纳为发件人发来的无约束函件中的失败。 因此,它也涉及与客户有关的交叉要求和其他理由。

W3 XMLHttpRequest specification section 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.

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

www.un.org/Depts/DGACM/index_spanish.htm EDIT: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.

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.

现在,这只是一个udge子,甚至可能不会再工作,但我确实不记得这些问题的复杂性,但希望它会引导其他人走上正确的道路。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签