Does an HTTP Status code of 0 have any meaning?

127,340

Solution 1

Short Answer

It's not a HTTP response code, but it is documented by WhatWG as a valid value for the status attribute of an XMLHttpRequest or a Fetch response.

Broadly speaking, it is a default value used when there is no real HTTP status code to report and/or an error occurred sending the request or receiving the response. Possible scenarios where this is the case include, but are not limited to:

  • The request hasn't yet been sent, or was aborted.
  • The browser is still waiting to receive the response status and headers.
  • The connection dropped during the request.
  • The request timed out.
  • The request encountered an infinite redirect loop.
  • The browser knows the response status, but you're not allowed to access it due to security restrictions related to the Same-origin Policy.

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

The status-code element is a three-digit integer code

which 0 is not.

However, 0 as a value of the .status attribute of an XMLHttpRequest object is documented, although it's a little tricky to track down all the relevant details. We begin at https://xhr.spec.whatwg.org/#the-status-attribute, documenting the .status attribute, which simply states:

The status attribute must return the response’s status.

That may sound vacuous and tautological, but in reality there is information here! Remember that this documentation is talking here about the .response attribute of an XMLHttpRequest, not a response, so this tells us that the definition of the status on an XHR object is deferred to the definition of a response's status in the Fetch spec.

But what response object? What if we haven't actually received a response yet? The inline link on the word "response" takes us to https://xhr.spec.whatwg.org/#response, which explains:

An XMLHttpRequest has an associated response. Unless stated otherwise it is a network error.

So the response whose status we're getting is by default a network error. And by searching for everywhere the phrase "set response to" is used in the XHR spec, we can see that it's set in five places:

Looking in the Fetch standard, we can see that:

A network error is a response whose status is always 0

so we can immediately tell that we'll see a status of 0 on an XHR object in any of the cases where the XHR spec says the response should be set to a network error. (Interestingly, this includes the case where the body's stream gets "errored", which the Fetch spec tells us can happen during parsing the body after having received the status - so in theory I suppose it is possible for an XHR object to have its status set to 200, then encounter an out-of-memory error or something while receiving the body and so change its status back to 0.)

We also note in the Fetch standard that a couple of other response types exist whose status is defined to be 0, whose existence relates to cross-origin requests and the same-origin policy:

An opaque filtered response is a filtered response whose ... status is 0...

An opaque-redirect filtered response is a filtered response whose ... status is 0...

(various other details about these two response types omitted).

But beyond these, there are also many cases where the Fetch algorithm (rather than the XHR spec, which we've already looked at) calls for the browser to return a network error! Indeed, the phrase "return a network error" appears 40 times in the Fetch standard. I will not try to list all 40 here, but I note that they include:

  • The case where the request's scheme is unrecognised (e.g. trying to send a request to madeupscheme://foobar.com)
  • The wonderfully vague instruction "When in doubt, return a network error." in the algorithms for handling ftp:// and file:// URLs
  • Infinite redirects: "If request’s redirect count is twenty, return a network error."
  • A bunch of CORS-related issues, such as "If httpRequest’s response tainting is not "cors" and the cross-origin resource policy check with request and response returns blocked, then return a network error."
  • Connection failures: "If connection is failure, return a network error."

In other words: whenever something goes wrong other than getting a real HTTP error status code like a 500 or 400 from the server, you end up with a status attribute of 0 on your XHR object or Fetch response object in the browser. The number of possible specific causes enumerated in spec is vast.

Finally: if you're interested in the history of the spec for some reason, note that this answer was completely rewritten in 2020, and that you may be interested in the previous revision of this answer, which parsed essentially the same conclusions out of the older (and much simpler) W3 spec for XHR, before these were replaced by the more modern and more complicated WhatWG specs this answers refers to.

Solution 2

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.

Solution 3

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):

const unsigned short UNSENT = 0;
const unsigned short OPENED = 1;
const unsigned short HEADERS_RECEIVED = 2;
const unsigned short LOADING = 3;
const unsigned short DONE = 4;

Solution 4

from documentation http://www.w3.org/TR/XMLHttpRequest/#the-status-attribute means a request was cancelled before going anywhere

Share:
127,340

Related videos on Youtube

Mark Lutton
Author by

Mark Lutton

As a kid I was fascinated with computers. I read books about them and made a paper Turing machine. At Boston University, I got an account on the computer, took a course in Fortran and programmed it with the rules of counterpoint from Professor Norden’s book. After graduating with a degree in music, I found there wasn’t much of a market for concert pianists but there was for super-fast key data entry operators, so I took a job at a computer service bureau and soon moved up to operating the IBM 370. In my spare time I programmed an ancient computer to play music through a radio. Soon I moved into programming, becoming expert in COBOL, Fortran and Assembler. That was just the start of a long and successful career. I have worked with Windows, Linux, Java, J2EE, Tomcat, Struts, JavaScript, Dojo, and dabbled a bit with Groovy, Gradle, EasyMock, Selenium, and too many others to remember. I consider myself strongest in Java, J2EE, JavaScript and Python, but I’ll learn anything you need. The improvements to JavaScript in the past few years are remarkable: with the asynchronous facilities you can use a shared-nothing message-passing design that avoids the old concurrency problems. Now you may be wondering why I haven’t written articles and attended a lot of conferences and made a name for myself in the software industry. Well, I do have a pretty good reputation here on StackOverflow so there’s that. But mainly I spent the time practicing the piano and organ. I played in competitions from time to time. In 2008 I went on a concert tour with flutist Cassie White, playing in Weill Recital Hall at Carnegie Hall. Our CD is available from Amazon.com and on iTunes and Spotify. Back in 2016 I decided to take a break. I left Cisco and went back to college part time. In May, 2020 I got my Associate degrees in Accounting and Small Business Management, Summa Cum Laude, at Nashua Community College, so I’m a recent college graduate! Companies are ALWAYS looking for recent college graduates. I have also started on a Master of Science degree in Accounting online at Southern New Hampshire University. So far I have taken undergrad courses in Statistics, Financial Accounting, Cost Accounting, Intermediate Accounting, Management, Sales, Advertising, Small Business Management, Desktop Applications, Business Law, and HR Management, and graduate-level courses in Financial Reporting, Math and Stats for Business, Federal Taxation of Individuals, Economics (both micro and macro), and Auditing. Coming up next: Corporate Financial Management. I have a 4.0 GPA in all these courses. Your takeaway from this is that I still love to learn things, I get things done on time and with the highest quality, and I am particularly adept at finding and fixing bugs. Now it’s time for me to get back into software engineering. Got some tickets you need closed?

Updated on January 05, 2020

Comments

  • Mark Lutton
    Mark Lutton over 4 years

    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?

  • Ankur
    Ankur about 11 years
    Hello, Is there any way to differentiate between failed("No Network") and cancelled request?
  • Mark Amery
    Mark Amery over 9 years
    That's one of a few possible meanings listed.
  • Frédéric
    Frédéric over 9 years
    This status is documented as being a XmlHttpRequest status. Of course it is not a http status, but it is documented. w3.org/TR/XMLHttpRequest/#the-status-attribute See Mark Amery response for more details.
  • Mark Amery
    Mark Amery over 6 years
    All possible causes in your list here are correct, but your list is not exhaustive. For instance, you're missing timeouts, infinite redirect loops, and other causes detailed in my answer. However, the pointer to the new living XHR spec is useful; I should update my answer to cite that instead of the old W3 spec.