Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    XMLHttpRequest readyState is 0

    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/...p-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!

  • #2
    Regular Coder
    Join Date
    Aug 2005
    Posts
    282
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if you post up the code, perhaps someone can assist you better?

  • #3
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!
    Code:
    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 Code:
    <?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>
    Last edited by jalawesome; 06-03-2006 at 07:30 AM.

  • #4
    Regular Coder
    Join Date
    Aug 2005
    Posts
    282
    Thanks
    0
    Thanked 0 Times in 0 Posts
    what/where is your pauseComp function?
    and why are you using it here?

  • #5
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Change this in the loadXMLDoc function:
    Code:
    req.onreadystatechange = processReqChange();
    to
    Code:
    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
    "Failure is not an option. It comes bundled with the software."
    ....../)/)..(\__/).(\(\................../)_/)......
    .....(-.-).(='.'=).(-.-)................(o.O)...../<)
    ....(.).(.)("}_("}(.)(.)...............(.)_(.))Ż/.
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
    Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.

  • #6
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That did it! Thank you so much!!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •