It appears that when you make an XMLHttpRequest from a script in a browser, if the browser is set to work offline or if the network cable is pulled out, the request completes with an error and with status = 0. 0 is not listed among permissible HTTP status codes.
What does a status code of 0 mean? Does it mean the same thing across all browsers, and for all HTTP client utilities? Is it part of the HTTP spec or is it part of some other protocol spec? It seems to mean that the HTTP request could not be made at all, perhaps because the server address could not be resolved.
What error message is appropriate to show the user? "Either you are not connected to the internet, or the website is encountering problems, or there might be a typing error in the address"?
I should add to this that I see the behavior in FireFox when set to "Work Offline", but not in Microsoft Internet Explorer when set to "Work Offline". In IE, the user gets a dialog giving the option to go online. FireFox does not notify the user before returning the error.
I am asking this in response to a request to "show a better error message". What Internet Explorer does is good. It tells the user what is causing the problem and gives them the option to fix it. In order to give an equivalent UX with FireFox I need to infer the cause of the problem and inform the user. So what in total can I infer from Status 0? Does it have a universal meaning or does it tell me nothing?
Know it's an old post. But these issues still exist.
Here are some of my findings on the subject, grossly explained.
"Status" 0 means one of 3 things, as per the XMLHttpRequest spec:
dns name resolution failed (that's for instance when network plug is pulled out)
server did not answer (a.k.a. unreachable or unresponding)
request was aborted because of a CORS issue (abortion is performed by the user-agent and follows a failing OPTIONS pre-flight).
If you want to go further, dive deep into the inners of XMLHttpRequest. I suggest reading the ready-state update sequence ([0,1,2,3,4] is the normal sequence, [0,1,4] corresponds to status 0, [0,1,2,4] means no content sent which may be an error or not). You may also want to attach listeners to the xhr (onreadystatechange, onabort, onerror, ontimeout) to figure out details.
From the spec (XHR Living spec):
from documentation http://www.w3.org/TR/XMLHttpRequest/#the-status-attribute means a request was cancelled before going anywhere
Since iOS 9, you need to add "App Transport Security Settings" to your info.plist file and allow "Allow Arbitrary Loads" before making request to non-secure HTTP web service. I had this issue in one of my app.
Yes, some how the ajax call aborted. The cause may be following.
status 0 appear when an ajax call was cancelled before getting the response by refreshing the page or requesting a URL that is unreachable.
this status is not documented but exist over ajax and makeRequest call's from gadget.io.
Short Answer
It's not a HTTP response code, but it is documented by W3 as a valid value for the
status
attribute of anXMLHttpRequest
(and thus also of ajqXHR
object, for jQuery users).It covers a whole swathe of possible situations in which there's no real HTTP response code available to report, either because you haven't sent the request, you explicitly aborted it, the page is unloading, or x went wrong for one of many possible values of x.
Long Answer
First, to reiterate: 0 is not a HTTP status code. There's a complete list of them in RFC 7231 Section 6.1, that doesn't include 0, and the intro to section 6 states clearly that
which 0 is not.
However, 0 as a value of the
status
attribute of anXMLHttpRequest
object is documented. From documentation at http://www.w3.org/TR/XMLHttpRequest/#the-status-attribute:We can dig deeper into the spec and find out just what those conditions for returning 0 mean. From http://www.w3.org/TR/XMLHttpRequest/#states:
It's also relevant to note that the next possible state after
UNSENT
andOPENED
isHEADERS_RECEIVED
:Putting this all together, the short answer is that 0 is simply what gets returned by the
status
attribute of anXMLHttpRequest
object when there is no real status code to return, because either:Okay, but what errors can cause this mysterious "error flag" to be set? If you CTRL-F for 'error flag' in the W3 documentation, you will find that it gets unset upon sending the request, and it only ever gets set as part of the algorithm to "terminate the request". Looking for all the places that algorithm is invoked, you'll find that it happens when:
open()
methodabort()
methodA 'request error' happens, which can happen when one of the following situations occurs:
A network error occurs, which can happen if
An abort error occurs, which can only happen if
whatever that means. I don't know of any browser that shows users when AJAX requests are occurring and gives them the opportunity to cancel them explicitly, so I think this one is - at least today - irrelevant.
A timeout error occurs, which means, reasonably enough, that
As far as
XMLHttpRequest
goes, that's everything.Beyond
XMLHttpRequest
, I would speculate that HTTP libraries in languages outside of JavaScript may well be using a 0 status code similarly, as a default value when no status code has been received from the server.