Skip to content Skip to sidebar Skip to footer

XMLHttpRequest Status 0 On Second Load

I am experiencing an interesting issue when I am trying to load some data in .txt format from the same domain using XMLHttpRequest. I am trying to load the data, parse it and then

Solution 1:

There are two causes of status code of zero.

  1. Making calls from the file protocol.
  2. The page is refreshing/navigating away as the request is being made.

In your case I would assume it is #2. If you are using a button or a link to make the Ajax call, make sure to cancel the click action with either preventDefault or return false.


Solution 2:

Sounds like a caching issue. Try either switching to a POST method, or appending a timestamp to the GET request querystring and see if that prevents the caching.

xmlhttp.open("POST", "data/somedata.txt", false); 

or:

xmlhttp.open("GET", "data/somedata.txt?" + new Date().valueOf(), false);

Edit: If those don't work, modify your server configuration to send appropriate response headers for that file or type to not cache the response. Ie: Cache-Control: no-cache


Solution 3:

Try xmlhttp.abort() before opening a new request.

It's a long shot but worth the try.


Post a Comment for "XMLHttpRequest Status 0 On Second Load"