...

View Full Version : XMLHttpRequest readyState is 0



jalawesome
06-03-2006, 01:13 AM
I am trying to use XMLHttpRequest for the first time. I followed several tutorials such as the one at http://www.xml.com/pub/a/2005/02/09/xml-http-request.html
My readystate never changes from zero. I even put a pause in my script and had it wait 5 seconds, and it is still 0. If i put the url for the xml file in FF it displays fine. I am using GET with two parameters, and the php checks a database, which executes perfectly. My script is on the same domain as the page, which I heard it must be. Any ideas what might be causing this? Thanks!

KC-Luck
06-03-2006, 01:53 AM
if you post up the code, perhaps someone can assist you better?

jalawesome
06-03-2006, 07:10 AM
Yeah, I just thought I would see if this was a common problem that someone might have a solution to before I posted code. Here it is. I am testing this in firefox. I know I have to put in branching code for IE to work with...but I wanted to get one browser working first. Thanks!

function checkAnswer()
{
picID = document.getElementById("picID").value;
resp = document.getElementById("resp").value;
alert("checking answer "+resp+" for picture "+picID);
picID = picID.split(".")[0];
checkCorrect(picID,resp,'');
}


function checkCorrect(picID, resp, correct){
if(correct != ''){
if (correct == 0){
alert("That is not correct, please select another answer");
}else if (correct == 1){
alert("That is correct!");
loadnext();
}
}else{
url = 'http://mathcs.muhlenberg.edu/leaf/tutorialresponse.php?picID='+picID+'&resp='+resp;
alert("loading: "+url);
loadXMLDoc(url);
}
}


function loadXMLDoc(url)
{
req = new XMLHttpRequest();
req.onreadystatechange = processReqChange();
req.open("GET", url, true);
req.send("");
}

function processReqChange()
{
pauseComp(5000);
alert("readyState: "+req.readyState+" status: "+req.status);
if(req.readyState == 4){
alert("checking for status 200...");
if(req.status == 200){
alert("XMLHttpRequest object is ready!");
response = req.responseXML.documentElement;
method = response.getElementsByTagName('method')[0].firstChild.data;
result = response.getElementsByTagName('result')[0].firstChild.data;
eval(method + '(\'\',\'\',result)');
}else{
alert("there was a problem!");
}
}
}

<?php
header('Content-Type: text/xml');

function isCorrect($picID, $resp){

//Connect to Leaf Database

$leaf = mysql_connect("localhost", "user", "pass");

if(!$leaf){
print "Error - Could Not Connect to Leaf Database";
exit;
}

$err = mysql_select_db("leaf");

if(!$err){
print "Error - Could Not Select the Database";
exit;
}

$qry = "SELECT correct FROM Tutorial WHERE pic='$picID';";

$result = mysql_query($qry);
$correct = mysql_result($result,0,"correct");

mysql_close($leaf);

if($correct == $resp){
return 1;
}
else{
return 0;
}
}
?>
<?php echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'; ?>
<response>
<method>checkCorrect</method>
<result><?php echo isCorrect($_GET['picID'],$_GET['resp']) ?></result>
</response>

KC-Luck
06-03-2006, 04:18 PM
what/where is your pauseComp function?
and why are you using it here?

dumpfi
06-03-2006, 04:27 PM
Change this in the loadXMLDoc function:
req.onreadystatechange = processReqChange();to
req.onreadystatechange = processReqChange;You are executing the processReqChange function when you call loadXMLDoc and then assign the return value to req.onreadystatechange, which of course is wrong. You must instead assign the processReqChange function to req.onreadystatechange.

dumpfi

jalawesome
06-03-2006, 06:50 PM
That did it! Thank you so much!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum