Jquery ajaxrequest xhr.status code 0 but html status code 200
Asked Answered
P

2

5

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

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.

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

Any ideas?

Psf answered 12/1, 2012 at 9:0 Comment(4)
Have you tried loading alternate resources? Like a static html file for example. WHat is the content type of your response from your php file?Presbyter
Yup and yup. The content of my response file is pure html.Psf
Tried to update jquery to the latest version as well, though I doubted it would matter. And the problem remains.Psf
Sorry not the markup of your document bu the response header ContentType - what have you set it to when your php script responds?Presbyter
D
10

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.1 states:

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

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.

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.

Dallapiccola answered 27/1, 2012 at 19:18 Comment(4)
After working on this for about 8 hours, I was able to find a way (kludge) to determine if the connection was aborted or if the status code 0 was caused by some other failure. I am using jQuery 1.4.x and after viewing their source code and the XMLHttpRequest specification, I do have to say that I think the jQuery implementation is pretty poor for providing all the necessary feedback to the developer. Hopefully that has been fixed in future versions. If you're curious about my implementation, let me know.Dallapiccola
+1 from me. This helped me to find my problem. As mentioned I was navigating to another page before the response of the async call comes. I've wasted almost a day to find the issue and thanks a lot for the help.Psi
yes @loesak, for all that is good and beauty in the world, could you share your solution? 0: )Addressograph
@Addressograph unfortunately I no longer work at the company where i fixed this issue. It's also been a few years since so my memory is a bit vague. I'll edit my answer to provide a summary.Dallapiccola
K
3

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.

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’

Karns answered 12/1, 2012 at 11:19 Comment(3)
The requested page as well as the requesting one is located on the same server, and as can be seen above in my submitted code, a relative url is used!Psf
The localhost part of my answer was crucial here - maybe you're developing on your local machine (and have the server there as well). In my case that was an issue, and I had to treat status code 0 as 200 for as long as I was developing on local machine. Once I deployed application on remote server response code of previously "broken" ajax requests was back to normal (200).Karns
Aha! Unfortunatley the problem exists both on my local mashine and on the deployment server.Psf

© 2022 - 2024 — McMap. All rights reserved.