By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,083 Members | 1,957 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,083 IT Pros & Developers. It's quick & easy.

readyState always = 1??? Why

P: 8
I recently try to learn Ajax. But my code never got pass the xmlhttp.readyState because it is always = to 1. My callback function is OnData and it is call when onreadystatechange set. Line 40 is where I check readyState.
Here is my code. Please help. Thank you
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. function get_message(){
  3.     return "Hello World! It is ".date("r");
  4. }
  5. if(isset($_GET["NOW"])){
  6.     sleep(5);
  7.     header("Cache-Control: no-cache, must-revalidate");
  8.     header("Content-type: text/plain");
  9.     echo get_message();
  10. }else {?>
  11. <html>
  12. <head>
  13.  
  14.     <script type="text/javascript">
  15.         var xmlhttp = null;
  16.         function OnLoad(){
  17.             document.getElementById("action").href="javascript:Update()";
  18.         }
  19.         function Update(){
  20.             if(xmlhttp!= null) return;
  21.             document.getElementById("data").innerHTML = "Loading";
  22.  
  23.             xmlhttp = NewHTTP();
  24.  
  25.             xmlhttp.open("GET", "?NOW", false);
  26.  
  27.             xmlhttp.onreadystatechange = OnData;
  28.             xmlhttp.send(null);
  29.  
  30.         }
  31.         function NewHTTP(){
  32.             try{
  33.                 return new XMLHttpRequest();
  34.             }catch(e){
  35.                 return new ActiveXObject("Msxml2.XMLHTTP.3.0");
  36.             }
  37.         }
  38.         function OnData(){
  39.             m = document.getElementById("data");
  40.             if(xmlhttp.readyState == 4){
  41.                 document.write("inside the first if");
  42.                 if(xmlhttp.status == 200){
  43.                     m.innerHTML = xmlhttp.responseText;
  44.                 }else{
  45.                     m.innerHTML = "Error loading date and time";
  46.                 }
  47.             }
  48.             xmlhttp = null;
  49.         }
  50.  
  51.     </script>
  52.  
  53. </head>
  54. <body onLoad = "OnLoad();">
  55.         <div id="data">
  56.             <?
  57.             if(isset($_GET["NOW-INLINE"])){
  58.                 echo get_message();
  59.             }else
  60.                 echo "Press Go";
  61.             ?>
  62.         </div>
  63.         <a href="?NOW-INLINE" id="action">Go</a>
  64. </body>
  65.  
  66. </html>
  67. <?php } ?>
  68.  
Jun 12 '07 #1
Share this Question
Share on Google+
5 Replies


ronnil
Expert 100+
P: 134
try setting async to true in

xmlhttp.open("GET", "?NOW", false);

so it becomes

xmlhttp.open("GET", "?NOW", true);

the "?NOW" could confuse whichever browser you're working in. try supplying the filename, at least until you got it working properly
Jun 12 '07 #2

ak1dnar
Expert 100+
P: 1,584
I recently try to learn Ajax. But my code never got pass the xmlhttp.readyState because it is always = to 1. My callback function is OnData and it is call when onreadystatechange set. Line 40 is where I check readyState.
Here is my code. Please help. Thank you
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. function get_message(){
  3.     return "Hello World! It is ".date("r");
  4. }
  5. if(isset($_GET["NOW"])){
  6.     sleep(5);
  7.     header("Cache-Control: no-cache, must-revalidate");
  8.     header("Content-type: text/plain");
  9.     echo get_message();
  10. }else {?>
  11. <html>
  12. <head>
  13.  
  14.     <script type="text/javascript">
  15.         var xmlhttp = null;
  16.         function OnLoad(){
  17.             document.getElementById("action").href="javascript:Update()";
  18.         }
  19.         function Update(){
  20.             if(xmlhttp!= null) return;
  21.             document.getElementById("data").innerHTML = "Loading";
  22.  
  23.             xmlhttp = NewHTTP();
  24.  
  25.             xmlhttp.open("GET", "?NOW", false);
  26.  
  27.             xmlhttp.onreadystatechange = OnData;
  28.             xmlhttp.send(null);
  29.  
  30.         }
  31.         function NewHTTP(){
  32.             try{
  33.                 return new XMLHttpRequest();
  34.             }catch(e){
  35.                 return new ActiveXObject("Msxml2.XMLHTTP.3.0");
  36.             }
  37.         }
  38.         function OnData(){
  39.             m = document.getElementById("data");
  40.             if(xmlhttp.readyState == 4){
  41.                 document.write("inside the first if");
  42.                 if(xmlhttp.status == 200){
  43.                     m.innerHTML = xmlhttp.responseText;
  44.                 }else{
  45.                     m.innerHTML = "Error loading date and time";
  46.                 }
  47.             }
  48.             xmlhttp = null;
  49.         }
  50.  
  51.     </script>
  52.  
  53. </head>
  54. <body onLoad = "OnLoad();">
  55.         <div id="data">
  56.             <?
  57.             if(isset($_GET["NOW-INLINE"])){
  58.                 echo get_message();
  59.             }else
  60.                 echo "Press Go";
  61.             ?>
  62.         </div>
  63.         <a href="?NOW-INLINE" id="action">Go</a>
  64. </body>
  65.  
  66. </html>
  67. <?php } ?>
  68.  
I made some changes to the original :

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.       function get_message()
  3.       {
  4.           return "Hello World! It is ".date("r");
  5.       }
  6.  
  7.       if(isset($_GET["NOW"]))
  8.       {
  9.  
  10.           sleep(5);
  11.  
  12.           header("Cache-Control: no-cache, must-revalidate");
  13.  
  14.           header("Content-type: text/plain");
  15.  
  16.           echo get_message();
  17.  
  18.       }
  19.       else 
  20.       {
  21.       ?>
  22.  
  23.       <html>
  24.  
  25.       <head>
  26.  
  27.  
  28.  
  29.           <script type="text/javascript">
  30.          var xmlhttp = null;
  31.  
  32.               function OnLoad()
  33.               {
  34.  
  35.                   document.getElementById("action").href="javascript:Update()";
  36.  
  37.               }
  38.  
  39.               function Update(){
  40.  
  41.                   if(xmlhttp!= null) return;
  42.  
  43.                  // document.getElementById("data").innerHTML = "Loading";
  44.  
  45.  
  46.  
  47.                   xmlhttp = NewHTTP();
  48.  
  49.  
  50.  
  51.                   xmlhttp.open("GET", "this_page.php?NOW", true);
  52.  
  53.  
  54.  
  55.                   xmlhttp.onreadystatechange = OnData;
  56.  
  57.                   xmlhttp.send(null);
  58.  
  59.  
  60.  
  61.               }
  62.  
  63.               function NewHTTP(){
  64.  
  65.                   try{
  66.  
  67.                       return new XMLHttpRequest();
  68.  
  69.                   }catch(e){
  70.  
  71.                       return new ActiveXObject("Msxml2.XMLHTTP.3.0");
  72.                   }
  73.               }
  74.               function OnData()
  75.               {
  76.  
  77.                   m =  document.getElementById("data");
  78.  
  79.                   if(xmlhttp.readyState != 4)
  80.                   {
  81.                   m.innerHTML = 'Loading....';
  82.                   }
  83.                   if(xmlhttp.readyState == 4){
  84.  
  85.                       //document.write("inside the first if");
  86.  
  87.                       if(xmlhttp.status == 200)
  88.                       {
  89.  
  90.                           m.innerHTML = xmlhttp.responseText;
  91.  
  92.                       }
  93.                       else
  94.                       {
  95.  
  96.                           m.innerHTML = "Error loading date and time";
  97.  
  98.                       }
  99.  
  100.                   }
  101.  
  102.                   //xmlhttp = null;
  103.  
  104.  
  105.               }
  106.  
  107.  
  108.  
  109.           </script>
  110.  
  111.  
  112.  
  113.       </head>
  114.  
  115.       <body onLoad = "OnLoad();">
  116.  
  117.               <div id="data">
  118.  
  119.                   <?
  120.  
  121.                   if(isset($_GET["NOW-INLINE"])){
  122.  
  123.                       echo get_message();
  124.  
  125.                   }else
  126.  
  127.                       echo "Press Go";
  128.  
  129.                   ?>
  130.  
  131.               </div>
  132.  
  133.               <a href="?NOW-INLINE" id="action">Go</a>
  134.  
  135.       </body>
  136.  
  137.  
  138.  
  139.       </html>
  140.  
  141.       <?php } ?>
Jun 13 '07 #3

P: 8
Thank you so much. I will try
Jun 13 '07 #4

P: 8
it work fabulous. Thank you so much
Jun 13 '07 #5

ak1dnar
Expert 100+
P: 1,584
it work fabulous. Thank you so much
Glad to hear that !

Since you are a beginner for AJAX hope these tutorials help you.
Jun 14 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.