ajax - Does an HTTP Status code of 0 have any meaning? -
it appears when make xmlhttprequest script in browser, if browser set work offline or if network cable pulled out, request completes error , status = 0. 0 not listed among permissible http status codes.
what status code of 0 mean? mean same thing across browsers, , http client utilities? part of http spec or part of other protocol spec? seems mean http request not made @ all, perhaps because server address not resolved.
what error message appropriate show user? "either not connected internet, or website encountering problems, or there might typing error in address"?
i should add see behavior in firefox when set "work offline", not in microsoft internet explorer when set "work offline". in ie, user gets dialog giving option go online. firefox not notify user before returning error.
i asking in response request "show better error message". internet explorer good. tells user causing problem , gives them option fix it. in order give equivalent ux firefox need infer cause of problem , inform user. in total can infer status 0? have universal meaning or tell me nothing?
short answer
it's not http response code, is documented w3 valid value status
attribute of xmlhttprequest
(and of jqxhr
object, jquery users).
it covers whole swathe of possible situations in there's no real http response code available report, either because haven't sent request, explicitly aborted it, page unloading, or x went wrong 1 of many possible values of x.
long answer
first, reiterate: 0 not http status code. there's complete list of them in rfc 7231 section 6.1, doesn't include 0, , intro section 6 states that
the status-code element three-digit integer code
which 0 not.
however, 0 value of status
attribute of xmlhttprequest
object is documented. documentation @ http://www.w3.org/tr/xmlhttprequest/#the-status-attribute:
4.7.1 status attribute
the
status
attribute must return result of running these steps:
if state unsent or opened, return 0.
if error flag set, return 0.
return http status code.
we can dig deeper spec , find out conditions returning 0 mean. http://www.w3.org/tr/xmlhttprequest/#states:
4.5 states
...
unsent
(numeric value 0)the object has been constructed.
opened
(numeric value 1)the
open()
method has been invoked. during state request headers can set usingsetrequestheader()
, request can made usingsend()
method....
the error flag indicates type of network error or fetch termination. unset.
it's relevant note next possible state after unsent
, opened
headers_received
:
headers_received
(numeric value 2)all redirects (if any) have been followed , http headers of final response have been received. several response members of object available.
putting together, short answer 0 gets returned status
attribute of xmlhttprequest
object when there no real status code return, because either:
- the request hasn't yet been sent, or
- the request has been sent headers of response have not yet been received, or
- one of many possible circumstances have occurred, listed in the docs, have caused "error flag" set.
okay, errors can cause mysterious "error flag" set? if ctrl-f 'error flag' in w3 documentation, find gets unset upon sending request, , ever gets set part of algorithm "terminate request". looking places that algorithm invoked, you'll find happens when:
- the request opened (or re-opened)
open()
method - the request garbage collected (e.g. when leaving page)
- the request aborted
abort()
method a 'request error' happens, can happen when 1 of following situations occurs:
a network error occurs, can happen if
- there's infinite redirect loop
- there is/are
dns errors, tls negotiation failure, or other type of network error
- the request cors request, , response cannot shared
an abort error occurs, can happen if
the end user cancels request
whatever means. don't know of browser shows users when ajax requests occurring , gives them opportunity cancel them explicitly, think 1 - @ least today - irrelevant.
a timeout error occurs, means, reasonably enough, that
timeout
not 0 , since request started amount of milliseconds specifiedtimeout
has passed
as far xmlhttprequest
goes, that's everything.
beyond xmlhttprequest
, speculate http libraries in languages outside of javascript may using 0 status code similarly, default value when no status code has been received server.
Comments
Post a Comment