Asynchronous Javascript problem (uh-oh...)

I’m building an aj script that need to request an array from a php script. The php script is working perfectly, but the javascript is failing without even a single error. This is my first aj script, so it’s probably just a typo or something. The script is pretty much self-explanatory, so here it is.

function initRequest(tarDir) {

  alert("started Request!");

  if (!ajax) {
    alert("No ajax here!");
    return false;
  }
  alert(ajax);
  
  var targetDir = tarDir;
  alert(targetDir);
  
  // Store the request to be sent to the script
  var request = targetDir;
  alert(request);
  
  // Store the location of the script to index the directory
  var indexScript = "scripts/dirIndex.php";
  alert(indexScript);
  
  ajax.open('POST', indexScript, false);
  alert("open");
  ajax.onReadyStateChange = getFileArray;
  alert("onReadyStateChange");
  ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  alert("setRequestHeader");
  ajax.send(request);
  alert("send");
}
  
function getFileArray() {

  alert("Getting FileArray!");

  if(ajax.readyState == 4 && ajax.status == 200) {
    var fileArray = ajax.responseText;
    for (var i = 0; fileArray*; i++) {
      alert(fileArray*);
    }
  } else {
    alert(ajax.readyState + ajax.status);
  }
}

I also have some code that creates an ajax object with some browser detection; here it is:

var ajax = false;

if (window.XMLHttpRequest) {

  ajax = new XMLHttpRequest();
  
} else if (window.ActiveXObject) {
  
  try {
  
    ajax = new ActiveXObject("Msxml2.XMLHTTP");
    
  } catch (e1) {
  
    try {
    
      ajax = new ActiveXObject("Microsoft.XMLHTTP");
      
    } catch (e2) { }
  }
}

if (!ajax) {

  alert('Ajax has failed us!');
  
}

If there aren’t any actual problems with it, could someone give me a hypothetical reason why it doesn’t return anything at all? Thanks much.

The responseText property of the XMLHttpRequest object is just a string. You’re treating it as an array. I don’t know what format your response is in, but I will assume it is a javascript array. Try running this:


eval(ajax.responseText);

before you loop through your array. Assuming that you’ve checked the responseText and it “looks” correct, there’s a good chance this will solve your issue.