views:

20

answers:

1

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. :)

A: 

after first sent async request "d01" you redefines xmlHttp. I see two ways to fix. xmlHttp should be declared inside loop back (and updateMemory also) or use sync requests

xmlHttp.open("GET",url,false);

If you do not use updateMemory outside this code then can use it as

xmlHttp.onreadystatechange = function() {[...your updateMemory src...]}

Best development http://vitana-group.com

Universal
I made the two changes: xmlHttp.open("GET",url,false);and xmlHttp.onreadystatechange = function updateMemory(){ ... }But it didn't appear to work.
Brian