XMLHttpRequest status = 0 and XMLHttpRequest statusText = unknown

While trying to put a copy of a website I had onto a CD to be used at a conference I ran into a problem with some AJAX code I was using on some inline pop up definitions I had on the site. These pop ups are essentially definitions that appear when a user does a mouse over on certain words. Code worked on the web server, but when I ran it on my local machine it sort of just broke and stopped working. After a little trial and error I cam to the conclusion that the XMLHttpRequest (XHR) object requires that the code to be executed on a web server to run correctly. I determined this by checking the XMLHttpRequest.status and XMLHttpRequest.statusText properties at various points in the process.

In my code I was using the two basic functions below, with the addition of the two alert() at the beginning of the proccessXML() function:

function loadXMLDoc(url, callFunc, ignoreWhite) {
  functionToCall = callFunc;
  stripWS = ignoreWhite;
  xmlRequestObj = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP")
  xmlRequestObj.onreadystatechange = proccessXML;
  xmlRequestObj.open("GET", url, true);

function proccessXML() {
  alert(xmlRequestObj status);

  if (xmlRequestObj.readyState == 4 && (xmlRequestObj.status == 200 || xmlRequestObj.status == 304)) {
  resultXML = xmlRequestObj.responseXML;
  resultXMLisLoaded = 1;

First loadXMLDoc() is called with the url to the xml file passed as a parameter. The function continues on and creates an an XMLHttpRequest object called xmlRequestObj:

xmlRequestObj = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP")

Next an event handler is created that will call the function processXML() each time the object reaches a new level or state during its attempt to collect the xml stream being passed to it by the server (0 = uninitialized, 1 = open, 2 = sent, 3 = receiving, 4 = loaded or complete):

xmlRequestObj.onreadystatechange = proccessXML;

Then finally the XMLHttpRequest object is told where to get its stream from:

xmlRequestObj.open("GET", url, true);

When running the code on the server the two alerts would return the value 200, and the text OK.

When running the code locally I would get XMLHttpRequest status 0, and XMLHttpRequest statusText unknown.

I believe the XMLHttpRequest object needs to receive the header information (A.K.A the HTTP status response) for it run correctly. Since this response is not returned to the object on the local machine it does not, or will not process the xml code stream.

By Colin Pear, 10/25/2006 -- Back to Java Script Tutorials ans lessons learned.