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 Coder
    Join Date
    Aug 2007
    Posts
    33
    Thanks
    7
    Thanked 0 Times in 0 Posts

    ajax on page load

    Hey,

    I'm new to Ajax am trying to get ajax to get values from a jsp page when the html loads, but first time the page loads, it gives me an error saying "The data necessary to complete this operation is not yet available" for the xmlHTTPRequest.responseText but when i refresh the page, it loads perfectly! jsp tested and works so the problem resides in my ajax/html file.

    EDIT: this reloading thing only works in IE, in mozilla this page doesnt work at all

    heres my html (i dont know if I am doing this right though) any thoughts welcome and much appreciated:

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function ajaxFunction() { 
      var xmlHttp;
      var optionID;
    
      //generic code (www.w3schools.com)
      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;        
            }      
          }    
        }
            //problem 
              var url="get.jsp?name=";
              xmlHttp.open("GET", url,true);
              xmlHttp.send(null);
              var getVal = xmlHttp.responseText;
              alert(getVal);
              document.getElementById("myForm1").innerHTML=getVal;
      }
    
    </script>
    </head>
    <body onLoad="ajaxFunction()">
    <table name="mytable" cellspacing="10">
    <tr>
    <td>
    <form name="myForm" id="myForm1">
    </form>
    </td>
    <td>
    </td>
    </tr>
    </table>
    </body>
    </html>

  • #2
    Regular Coder
    Join Date
    Jul 2007
    Location
    UK
    Posts
    223
    Thanks
    0
    Thanked 14 Times in 14 Posts
    I guess you missed the bit about the onreadystatechange event handler in the tutorial you were using!

    Try this:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
     <head>
      <title> New Document </title>
      <script type="text/javascript">
    var xmlHttp;
    function ajaxFunction() { 
     
      var optionID;
      //generic code (www.w3schools.com)
      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;        
            }      
          }    
        }
            //problem 
              var url="test.xml";
              xmlHttp.open("GET", url,true);
        xmlHttp.onreadystatechange=handleAjaxResponse;
              xmlHttp.send(null);
     
      }
     function handleAjaxResponse(){
      if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
       var getVal = xmlHttp.responseText;
       alert(getVal);
       document.getElementById("myForm1").innerHTML=getVal;
      }
     }
    </script>
    </head>
    <body onLoad="ajaxFunction()">
    <table name="mytable" cellspacing="10">
    <tr>
    <td>
    <form name="myForm" id="myForm1">
    </form>
    </td>
    <td>
    </td>
    </tr>
    </table>
    </body>
    </html>
    Last edited by mcjwb; 08-27-2007 at 11:03 PM.

  • Users who have thanked mcjwb for this post:

    zenithwolf (08-27-2007)

  • #3
    New Coder
    Join Date
    Aug 2007
    Posts
    33
    Thanks
    7
    Thanked 0 Times in 0 Posts
    thanks a lot that works!

    however, can you explain to me the place (in Ajax) of the onreadystatechange because I don't quite understand why its there and how it works (thats why i thought it worked only when interacting with objects already there as opposed to loading a page)

    thanks

  • #4
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    As the request is being processed, it goes through diffreent states. The onreadystatechange event (logically named) fires each time the request's state changes. The states are as follows:

    0 = uninitialized
    1 = loading
    2 = loaded
    3 = interactive
    4 = complete

    When the readystate = 4 and everything went as expected, the status should be 200.

    Other codes will be returned if an error occured, such as 404 (page not found), 500 (error on the server side) etc.

    Also note that the readystate does not ever change if the request in synchronous in Firefox.

    I must ask, if you are doing this upon the load of the page, why use ajax at all? You could just utilize whatever server side language you would use normally and render your page as it loads just like the olden days.
    Helping to build a bigger box. - Adam Matthews

  • Users who have thanked Basscyst for this post:

    zenithwolf (08-28-2007)

  • #5
    New Coder
    Join Date
    Aug 2007
    Posts
    33
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Im totally new to AJAX (and javascripting) and trying to learn its various techniques, and I can do this other ways too (direct jsp, servlets etc) But I just wanted to do the whole thing in ajax since i have to dynamically render several DOM objects in my html and ajax seems the perfect solution, so i thought might as well experiment...and it works pretty nicely now

    also last thing, can you give me examples of when the other states (not 4) are fired? because I don't seem to understand why there are 4 more states if they only generate errors (if errors occur)

    thx for the replies
    Last edited by zenithwolf; 08-28-2007 at 05:33 AM.

  • #6
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    Quote Originally Posted by zenithwolf View Post
    ... can you give me examples of when the other states (not 4) are fired? because I don't seem to understand why there are 4 more states if they only generate errors (if errors occur)
    The documentation on readystate at MSDN may help you see what is going on.

  • Users who have thanked rwedge for this post:

    zenithwolf (08-28-2007)


  •  

    Posting Permissions

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