/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] [AJAX] multiple calls to an ajax application

first, id like to thank everyone who reads these and helps. out of all the forums i’ve ever been to with help for anything, these have proven to be the most reliable forums. i do my best to contribute, but often find that i have limited understanding of limited areas, and feel overwhelmed by how much the top contributers of this community know..

in regards to this issue, i need AJAX innerworking knowledge, rather than a coded solution (patch, if you will). so please try to help me learn rather than try to fix my problem with a coded solution! or, if my logic pattern is flawed, please suggest the best logical way to accomplish what i’ve set out to do. thanks!

THE TECHNICAL BREAKDOWN:

i use an ajax call to a php app (in my case, list_get.php) that returns a list of servers. each server consists of an IP and a PORT. the end of a server is denoted by a line return (“n”). for example:

[code]192.168.1.1:00001n[/code]

immediately after receiving my list of servers, i create an array by splitting based on the line return. for example:

[code]192.168.1.1:00001
192.168.1.1.00002
etc[/code]

after i have my array of servers, i go through each index of my array. during each iteration, i break the server into an IP and a PORT. at that time, i need to make another call to a different axaj app (in my case, list_show.php) in order to parse that particular IP and PORT combination.

up to this point, everything works perfectly. however, my problem occurs where i only see one response from my ajax application, despite there being multiple calls to the application.

i think my results are based on an incomplete understanding of the limitations of the “getxmlhttpobject”[COLOR=”Red”]*[/COLOR] function. given this, my observation (apparently based on my limited understanding of ajax) leads me to believe that i am interrupting the previous “getxmlhttpobject” request over and over, and only the last one has an opportunity to evaluate. am i correct?

[COLOR=”Red”]*[/COLOR]keep in mind that my reference to “getxmlhttpobject” is simply a call to an included function that creates the XML-object-thingydoo based on the browser’s support of it. you know, wether it’s MS, FIREFOX, OPERA, ETC.

[code=php]
<script type=”text/javascript” src=”include/GetXmlHttpObject.js”></script>

<script language=”javascript”>

var xmlHttp;

function getList()
{
xmlHttp = GetXmlHttpObject(); // returns MS,FF,OP object

if (xmlHttp==null)
{
alert (“Your browser does not support AJAX!”);
return;
}

var url = “list_get.php”;
url = url + “?sid=”+Math.random();

xmlHttp.onreadystatechange = getListChanged;
xmlHttp.open(“GET”,url,true);
xmlHttp.send(null);
}

function getListChanged()
{
if (xmlHttp.readyState==4)
{
var server;
var ip;
var port;

var response = xmlHttp.responseText.split(“n”);

for(x in response)
if(response[x] != “”)
{
server = response[x].split(“:”);

for(y in server)
switch(parseInt(y))
{
case 0:
ip = server[y];
break;
case 1:
port = server[y];
break;
//default:
// alert(typeof(y));
// break;
}

serverInfo(ip, port);
}
}
}

function serverInfo(ip, port)
{
alert(“server info was called with the following variables: ” + ip + “:” + port);

xmlHttp = GetXmlHttpObject();

if (xmlHttp==null)
{
alert (“Your browser does not support AJAX!”);
return;
}

var url = “list_show.php?”;
url += “server=” + ip + “&port=” + port;
url += “&sid=” + Math.random();

xmlHttp.onreadystatechange = serverInfoChanged;
xmlHttp.open(“GET”,url,true);
xmlHttp.send(null);
}

function serverInfoChanged()
{
if (xmlHttp.readyState==4)
{
output += xmlHttp.responseText + “|”;
}
}

getList();

</script>[/code]

(i only used the PHP tag cause it’s prettier – this is from a regular HTML page)

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@Angry_Black_ManauthorJul 03.2007 — one thing to note about my logic is that i WANT to call that app over and over and over. I want my page that will be outputting the results of my parsed servers to have the effect of being "updated".
Copy linkTweet thisAlerts:
@Angry_Black_ManauthorJul 04.2007 — ouch again. getting no responses hurts.

however, i was able to solve my quandary. my feeling that i was pre-empting previous httpresponses was correct. i re-coded it so that i did not call for my next httpresponse until my previous one was complete. i used a global variable to track my current place in my server array.

but once again, ouch.
Copy linkTweet thisAlerts:
@UltimaterJul 04.2007 — That's one way to do it.

Can also code it like such:
<i>
</i>xmlhttp=new Array();
for(...){
var l=xmlhttp.length;
xmlhttp[l]=GetXmlHttpObject();
xmlhttp[l].open(...);
xmlhttp[l].onreadystatechange=function(){...};
xmlhttp[l].send(null);
}

But I'd recommend sending PHP the complete array of servers to ping at once to save JavaScript from more than one XMLHttpRequest.
Copy linkTweet thisAlerts:
@Angry_Black_ManauthorJul 05.2007 — looks like i will have to send php all the information. it takes entirely too long to send the separate requests. it works perfectly now, but it just takes too freakin long. im very dissapointed.
×

Success!

Help @Angry_Black_Man spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...