56

I have a little problem understanding XMLHttpRequest's handlers. The specification says this about the onerror handler:

error [Dispatched ... ] When the request has failed.

load [Dispatched ... ] When the request has successfully completed.

The problem is, what does it mean that "the request has failed". That could be

  • the request couldn't be issued at all (eg. Connection refused and such errors), or
  • the above plus the server returned an error code (eg. 404)

Also, I'd like to know whether it means onerror and onload should never fire simultaneously.

This reference indicates the onerror handler should be executed depending on the status code and onload depending on readyState. That would indicate they are not mutually exclusive, however, I don't think this is an authoritative information.

I'm asking because using the latest Opera snapshot, I found onload is fired even on 404 status code. I know testing status is a sure bet, but I'd like to know whether it's something I have to do per specification or just a workaround for a bug in Opera.

2
  • 5
    I take has completed successfully to mean that you receive a status code, whether that be 200 OK or an error code such as 404. Chrome also fires onload even if the status code is an error status code. May 14, 2012 at 13:34
  • 2
    onerror and onload never fire simultaneously. It's either one or the other. However onloadend fires in both cases and is the last event in the row.
    – jayarjo
    Feb 22, 2013 at 15:58

2 Answers 2

84

As mentioned in the comments, onerror fires when there is a failure on the network level. If the error only exists on the application level, e.g., an HTTP error code is sent, then onload still fires. You need to test the returned status code explicitly in your onreadystatechange handler.

Note that a denied cross-domain request will also fire the onerror handler.

8
  • Hmm, strange. Can you (or anybody) explain that behavior of google server?
    – jpalecek
    May 14, 2012 at 18:56
  • I did a more complete test and discovered that encrypted.google.com doesn't send 404 for that request -- the status member of the XHR request is actually 0, where other servers send a 404. Apparently that server suddenly pretends that it doesn't exist at all when it sees a request for a page that doesn't exist. Weird, but that's Google's decision.
    – apsillers
    May 14, 2012 at 19:13
  • 7
    Or... it could be because hitting https://encrypted.google.com/foobar redirects to http://www.google.com/foobar, and the browser is refusing to participate in a cross-domain redirect during an Ajax request.
    – apsillers
    May 14, 2012 at 19:18
  • That would be it. The browser sends a request to http://www.google.com/foobar, sees there is no Access-Control-Allow-Origin header in the reply and refuses to give anything to the script.
    – jpalecek
    May 14, 2012 at 21:55
  • 2
    How do you distinguish what network level failure occurred? Several that I can think of are: DNS lookup failed, connection refused, timed out trying to contact server, timed out waiting for reply, denied due to CORS, and connection terminated abnormally.
    – Michael
    Dec 4, 2017 at 18:31
3

In addition to apsillers' answer, note that XMLHttpRequest handles redirects automatically in the background, so you don't have to check for this reply codes in the onload event (this event will be invoked only once - on the final call). Also note, that in case you send payload data with the POST method and if the requests is redirected, XMLHttpRequest change the method from POST to GET and discards any payload data for security reasons. The onload event will still be invoked but you will need to re-send your request manually again to the new destination.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.