Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 225
moiran Avatar asked Jan 12 '12 09:01

moiran


People also ask

What is XHR jQuery?

xhr (Function): Callback for creating the XMLHttpRequest object. Defaults to the ActiveXObject when available (IE), the XMLHttpRequest otherwise. Override to provide your own implementation for XMLHttpRequest or enhancements to the factory. It's not available in jQuery 1.2.

What is the major difference between jQuery Ajax and XMLHttpRequest XHR )?

XMLHttpRequest is the raw browser object that jQuery wraps into a more usable and simplified form and cross browser consistent functionality. jQuery. ajax is a general Ajax requester in jQuery that can do any type and content requests.

What does it mean when an HTTP request returns status code 0?

HTTP StatusCode=0 is associated with incomplete capture of a hit or page and often with a labeling of the hit as: request canceled ("ReqCancelled=Client" "ReqCancelled=Server" or "ReqCancelled=True").

What does a status of 0 mean?

As detailed by this answer on this page, a status code of 0 means the request failed for some reason, and a javascript library interpreted the fail as a status code of 0.


2 Answers

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’

like image 40
WTK Avatar answered Sep 28 '22 16:09

WTK


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.

like image 134
loesak Avatar answered Sep 28 '22 17:09

loesak