For the past day, I've been struggling with this issue and going in circles. Any help would be much appreciated :-)
Synopsis
I'm facing a challenge with asynchronous AJAX calls to CGI using resolver and FQDN variables to obtain DNS resolution results (essentially retrieving the output of `dig @resolver $FQDN`).
Obstacle
While Firebug indicates that GET requests are being fired asynchronously and responses are coming back as expected, I'm having trouble placing these responses into the correct DIVs within the document because the `onreadystatechange` function isn't recognizing the objects.
Additional Info
Even though I'm iterating through an array of objects, it seems they're all being fired instantly even when there's a delay between the iterations.
Below is the code snippet along with my comments:
Since resolver is an array, I've created an array of XMLHttpRequest objects.
function resolve() {
var numOfRes = 6;
var arr = new Array;
arr[0] = "192.168.1.11";
arr[1] = "8.8.8.8";
arr[2] = "8.8.4.4";
arr[3] = "159.134.0.1";
arr[4] = "159.134.0.2";
var len = arr.length;
var ax = new Array(); //creating ax as an array
for (var i=0; i<=len; i++) { //iterating through the length of resolvers array
ax[i] = new XMLHttpRequest(); //assigning to each item in array new object
//alert(ax[i]); // shows that object exists
ax[i].onreadystatechange = function(){
/*===
problem is above - firebug will show:
**Uncaught TypeError: Cannot read property 'readyState' of undefined**
**ax.(anonymous function).onreadystatechangehello.cgi:30**
oddly it will still populate divs inner html with 'loading +1 '
albeit regardless of readystate code (can be 4 or anything else )
It perplexes me why i is thought as a function?
=====*/
// alert(i); //if this is enabled I will see readyState==4 populated correctly
if (ax[i].readyState != 4) {
document.getElementById('return_table_'+i).innerHTML="loading "+i;
}
if(ax[i].readyState == 4){
// get data from the server response
var response_ready=ax[i].responseText;
document.getElementById('return_table_'+i).innerHTML = response_ready;
}
}
ax[i].open("GET","av.pl?resolver=" + arr[i] +"&fqdn=google.com",true); //works
ax[i].send(null); //works
}
}