View Full Version : XMLHttpRequest readyState is 0

06-03-2006, 12: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!

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

06-03-2006, 06: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];

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!");
url = 'http://mathcs.muhlenberg.edu/leaf/tutorialresponse.php?picID='+picID+'&resp='+resp;
alert("loading: "+url);

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

function processReqChange()
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)');
alert("there was a problem!");

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

function isCorrect($picID, $resp){

//Connect to Leaf Database

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

print "Error - Could Not Connect to Leaf Database";

$err = mysql_select_db("leaf");

print "Error - Could Not Select the Database";

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

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


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

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

06-03-2006, 03: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.


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