# Asynchronous Javascript problem (uh-oh...)

**URL:** <https://forum.kirupa.com/t/asynchronous-javascript-problem-uh-oh/266051>\
**Category:** web dev\
**Created:** [July 15, 2008, 8:34pm UTC](https://forum.kirupa.com/t/asynchronous-javascript-problem-uh-oh/266051 "2008-07-15T20:34:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sage\_of\_Fire](https://avatars.discourse-cdn.com/v4/letter/s/e95f7d/32.png) [@Sage\_of\_Fire](https://forum.kirupa.com/u/Sage_of_Fire)\
**Post date:** [July 15, 2008, 8:34pm UTC](https://forum.kirupa.com/t/asynchronous-javascript-problem-uh-oh/266051/1 "2008-07-15T20:34:39Z")

</div>

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.

```auto
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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![jwilliam](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@jwilliam](https://forum.kirupa.com/u/jwilliam)\
**Post date:** [July 15, 2008, 10:00pm UTC](https://forum.kirupa.com/t/asynchronous-javascript-problem-uh-oh/266051/2 "2008-07-15T22:00:54Z")

</div>

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:

```auto

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.
