...

View Full Version : No response(Text) from Ajax pt. 2



bilischan88
11-12-2007, 05:14 PM
Whenever I run my ajax script it seems to get caught up in the callback function.


function callback(id){
if (xmlHttp.readyState==4){
if(xmlHttp.status==200){
document.getElementById(id).innerHTML=xmlHttp.responseText;
}else{
document.getElementById(id).innerHTML="Cannot find page: " + xmlHttp.status;
}
}else{
document.getElementById(id).innerHTML="Waiting for server...";
}
}

It's supposed to display the responseText (obviously) but it always displays the "waiting for server" text and never moves on. When checking it in firebug it says it executed correctly and it even shows me the responseText that firebug says should've been displayed. However it doesn't display it in the browser. What's up with that?

BarrMan
11-12-2007, 05:26 PM
What's your other AJAX code? The one that is calling the function.
What's the element that is supposed to display the content?

bilischan88
11-12-2007, 06:26 PM
sorry, here's the function that's called by the event and calls the callback function.


function eventHorizon(place, which){
var dropdown = document.getElementById(which+'_country');
var choice = dropdown.selectedIndex;
var choice_text = dropdown.options[choice].text;
var num;
switch(choice_text){
default:
num = 0;
break
case 'United States of America':
num = 1;
break
case 'Canada':
num = 3;
break
case 'United Kingdom':
num = 2;
break
}
if(num != 0){
document.getElementById(place).innerHTML="Starting...";
xmlHttp=createXMLHttpRequest();
var url="u_info_supp.php?id="+num;
xmlHttp.onreadystatechange=callback(place)
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}else{
document.getElementById(place).innerHTML='';
}
}

here's the html.


<p id="place2"></p>
<p>Country: <select name="c_country" id="c_country" onchange="eventHorizon('place2', 'c')">
<?php
for($x=0; $x < count($countries[0]); $x++){
if(($countries[0][$x] == 'United States of America') || ($c_country == $countries[0][$x])){
echo "<option value=\"".$countries[0][$x]."\" selected=\"selected\">".$countries[0][$x]."</option>";
}else{
echo "<option value=\"".$countries[0][$x]."\">".$countries[0][$x]."</option>";
}
}
?></select></p>

id "place2" is where the response text goes.

BarrMan
11-12-2007, 06:55 PM
Try:

xmlHttp.onreadystatechange=function(){callback(place)};

bilischan88
11-12-2007, 07:16 PM
It didn't work.

BarrMan
11-12-2007, 07:52 PM
It didn't work.

And how's the content of the page u_info_supp.php viewed?
Have you checked the url?

bilischan88
11-12-2007, 10:02 PM
Actually your trick did work. The js script was just cached. It all works now. Thanks.
Just out of curiosity what's the difference b/w your way and mine.

BarrMan
11-12-2007, 10:27 PM
when you do var s=my_function(); it calls the function immediately when you write it but if you do var s=my_function; or var s=function(){my_function();}; it will call it only when you use the variable.

On this example it wouldn't quite work because the s variable is not a class or anything that needs to be called in order to initiate it but take for instance an onclick event, it would work there the same.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum