Help Needed With AJAX Script
- by Brian
Hello All
I am working on an AJAX script but am having difficulties. First, here is the script:
var xmlHttp;
function GetXmlHttpObject(){
var objXMLHttp=null
if (window.XMLHttpRequest){
objXMLHttp=new XMLHttpRequest()
}
else if (window.ActiveXObject){
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp
}
function retrieveData(){
var jBossServer = new Array();
jBossServer[0] = "d01";
jBossServer[1] = "d02";
jBossServer[2] = "p01";
jBossServer[3] = "p02";
for(var i=0; i<jBossServer.length; i++){
xmlHttp = GetXmlHttpObject();
if (xmlHttp == null){
alert ("Something weird happened ...");
return;
}
var url="./retrieveData.php";
url = url + "?jBossID=" + jBossServer[i];
url = url + "&sid=" + Math.random();
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange = updateMemory;
xmlHttp.send(null);
}
}
function updateMemory(){
if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete"){
aggregateArray = new Array();
aggregateArray = xmlHttp.responseText.split(',');
for(var i=0; i<aggregateArray.length; i++){
alert(aggregateArray[i]);
}
return;
}
}
The "retrieveData.php" page will return data that looks like this:
d01,1 MB,2 MB,3 MB
They relate to my 4 servers d01, d02, p01, and p02 (dev and prod). What I am doing is scraping the memory information from http://127.0.0.1:8080/web-console/ServerInfo.jsp
I want to save code so I attempted to put the AJAX xmlHttp call into a loop, but I don't think it is working. All I ever get back is the information for "p02" four times. Am I able to do what I want or do I need four different functions for each server (i.e., xmlHttpServer01, xmlHttpServer02, xmlHttpServer03, xmlHttpServer04)?
Thank you all for reading, have a good day. :)