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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Dec 2006
    Posts
    306
    Thanks
    24
    Thanked 0 Times in 0 Posts

    undifine the return value from the AJAX

    Hi;
    I am try to use the xmlHttp.responseText's value, but i find it some time works, some time does not work.

    in my code, i got some buttons as < 1 2 3 >.
    after i click the above buttons, some times it pop up a window
    with undifine
    , some times it pop up a window with the right value. This is my code's problem, could any one help me to solve this problem, please.

    index.html

    Code:
    <html>
    <body>
    
    <input type="button" id="b<" name="bb<" value="<" onClick="cButtons()"> 
    <input type="button" id="b1" name="bb1" value="1" onClick="cButtons()"> 
    <input type="button" id="b2" name="bb2" value="2" onClick="cButtons()"> 
    <input type="button" id="b3" name="bb3" value="3" onClick="cButtons()"> 
    <input type="button" id="b>" name="bb>" value=">" onClick="cButtons()"> 
    
    <script type="text/javascript">
       var rValue; 
    
    function cButtons(){
        ajaxFunction();
        alert(rValue);
    }
    
    
    function ajaxFunction()
      {
      var xmlHttp;
      try
        {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
        }
      catch (e)
        {
        // Internet Explorer
        try
          {
          xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
          }
        catch (e)
          {
          try
            {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
          catch (e)
            {
            alert("Your browser does not support AJAX!");
            return false;
            }
          }
        }
        xmlHttp.onreadystatechange=function()
          {
          if(xmlHttp.readyState==4)
            {
              rValue=xmlHttp.responseText;
             
            }
          }
        xmlHttp.open("GET","cateV.php",true);
        xmlHttp.send(null);
      }
    
    </script>
    
    </body>
    </html>

    cateV.php

    PHP Code:
    <?php
      $test
    ="i am testing";
      echo 
    $test;
    ?>

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    Try:
    Code:
        xmlHttp.open("GET","cateV.php",false);

  • #3
    Regular Coder
    Join Date
    Dec 2006
    Posts
    306
    Thanks
    24
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rwedge View Post
    Try:
    Code:
        xmlHttp.open("GET","cateV.php",false);
    it will pop up a window with undefine all the time, when i click the buttons.

    but if i declare a local varibal for that, it is not problem. as flollowing:
    Code:
    xmlHttp.onreadystatechange=function()
          {
       
          if(xmlHttp.readyState==4)
            {
               var myV;
               myV=xmlHttp.responseText;
               alert(myV);
             
            }
          }
    but for the global varibal, it does not work properly, sometimes it pop up a window with value, sometimes it pop up a window with undefine.
    Last edited by kamkam; 10-21-2007 at 10:34 AM.

  • #4
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    Code:
    var rValue; 
    function cButtons(){
        ajaxFunction();
        alert(rValue);
    }
    When the function cButtons() is called, with async set to true and the var rValue initalize as undefined, the alert(rValue) is executed reguardless of the ajaxFunction() being completed or not. With async set to false javascript is blocked until the request is completed.

    Set to true allows a better user experience as discussed here, but with your function cButtons() as it is, true can cause at least the first alert to be undefined.

    When you join the alert with the response
    Code:
    xmlHttp.onreadystatechange=function()
          {
          if(xmlHttp.readyState==4)
            {
               var myV;
               myV=xmlHttp.responseText;
               alert(myV);
            }
          }
    it is executed only when the response is completed.

  • #5
    Regular Coder
    Join Date
    Dec 2006
    Posts
    306
    Thanks
    24
    Thanked 0 Times in 0 Posts
    Thanks a lot, let me have some time to understand what you say.


  •  

    Posting Permissions

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