www.webdeveloper.com
Results 1 to 3 of 3

Thread: basic code not working...

  1. #1
    Join Date
    Aug 2010
    Posts
    4

    basic code not working...

    Hi All, I'm an AJAX beginner, I've copied a (basic) code from the book I'm reading and it behaves stubbornly wrong when I run it...
    here is the code that I think gives me the problem:
    Code:
    var http = getXMLHttpRequest(); // this gets an instance of XMLHttpRequest object. 
    
    function getServerTime(){
    
     var url = "time.php"; 
     var rdm = parseInt( Math.random()*999999 ); 
     var myURL=url+"?myRdm="+rdm; 
     http.open( "GET", myURL, true );
     http.onreadystatechange=useHttpResponse(); 
     http.send( null );  
    }
    
    function useHttpResponse(){
    
    alert ( "request status: "+ http.statusText ); 
    
    if( http.readyState == 4 ){ 
    alert(1); 
     if( http.status== 200 ){
    
    alert(2); 	
    	var timeNow = http.responsexml.getElementsByTagName( 'timenow' )[0]; 
    	var timeVal = timeNow.childNodes[ 0 ].nodeValue;
    alert( timeVal );  
    	document.getElementById( 'showtime' ).innerHTML = timeVal; 
     }else{
    	alert(3); 	
    	document.getElementById( 'showtime' ).innerHTML= http.statusText; 
     }
    }else{
    alert(4); 
     document.getElementById( 'showtime' ).innerHTML = "...waiting";   
    }
    }
    In the function useHttpResponse(),

    I expect some of the alerts to work, but none do. I can print out the http.readyState value ( which is always 1 ) but not http.status or http.statusText

    Please please advice....will be much appreciated.

    this is the php file: time.php
    PHP Code:
    <?php 
    header 
    'Content-Tye: text/xml' ); 
     echo 
    "<?xml version=\"1.0\" ?> <clock1> <timenow>"
    .date('H:i:s')."</timenow></clock1>"
    ?>

  2. #2
    Join Date
    Dec 2005
    Posts
    2,984
    Bad code if this is copy and paste - this is the problem line:
    Code:
    http.onreadystatechange=useHttpResponse();
    The onreadystatechange method of the http object should be set to a reference of the function that you use to handle the response. That way, every time the readystate property of the http object changes (and hence, every time the onreadystatechange method is called) it will point to the useHttpResponse function.

    In other words, change the above line to this ( without the () ):
    Code:
    http.onreadystatechange=useHttpResponse;
    EDIT: A little further: right now, you are setting http.onreadystatechange to the value returned by useHttpResponse by assigning it like this:
    Code:
    http.onreadystatechange=useHttpResponse();
    This tells the script to call that function and set http.onreadystatechange to it's return value (hence why you are getting the function called once and seeing an alert).
    But by using the syntax without the () you are setting http.onreadstatechange to a method handled by the function named useHttpResponse.....

    Hopefully that helps. A lot of people who have been using AJAX for a long time don't know the distinction and have just always left off the () because that's what they've always been doing.
    Last edited by aj_nsc; 08-24-2010 at 12:06 PM.

  3. #3
    Join Date
    Aug 2010
    Posts
    4
    Quote Originally Posted by aj_nsc View Post
    Bad code if this is copy and paste - this is the problem line:
    Code:
    http.onreadystatechange=useHttpResponse();
    The onreadystatechange method of the http object should be set to a reference of the function that you use to handle the response. That way, every time the readystate property of the http object changes (and hence, every time the onreadystatechange method is called) it will point to the useHttpResponse function.

    In other words, change the above line to this ( without the () ):
    Code:
    http.onreadystatechange=useHttpResponse;
    EDIT: A little further: right now, you are setting http.onreadystatechange to the value returned by useHttpResponse by assigning it like this:
    Code:
    http.onreadystatechange=useHttpResponse();
    This tells the script to call that function and set http.onreadystatechange to it's return value (hence why you are getting the function called once and seeing an alert).
    But by using the syntax without the () you are setting http.onreadstatechange to a method handled by the function named useHttpResponse.....

    Hopefully that helps. A lot of people who have been using AJAX for a long time don't know the distinction and have just always left off the () because that's what they've always been doing.

    A small step for man, a giant leap for mankind....
    ....yes, i can tell.
    Thank you.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles