www.webdeveloper.com
Results 1 to 3 of 3

Thread: Using AJAX to get data from a server

  1. #1
    Join Date
    Apr 2008
    Posts
    136

    Using AJAX to get data from a server

    I'm new to AJAX. Trying to write a program that requests a file from a server and displays the text in the requested file. Not having any luck. Nothing happens when I click on the button. Here's the code:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <body>
    <head>
    <title>Intro to AJAX--create and send an XMLHttpRequest object to the server</title>
    
    <script type="text/javascript" language="javascript">
    <!--The object detection code -->
    function goAjax()
    {
     var req = null;
    // Is there support for native XHR object?: IE7+, Firefox, Safari, Opera
     if (window.XMLHttpRequest)
    {
     req = new XMLHttpRequest();
     //create an XMLHttpRequest object
    }
    
     else if (window.ActiveXObject)
     //check for Version 6
    {
     req = new ActiveXObject('MSXML2.XMLHTTP.6.0');
     //create an ActiveX XMLHTTP component
    }
     if (!req)
    {
    req = new ActiveXObject('MSXML2.XMLHTTP');
    //fallback to version 3
    }
    //Request data to be retrieved from the server
    req.onreadystatechange = function() {
     if(req.readyState == 4) {
         document.form1.message.value = req.responseText; }
    else {
         document.form1.message.write("Retrieving message. One moment...");
         }
    }
    
    goAjax.open("GET", "message2.txt", "true");
    goAjax.send(null);
    
    }
    
    
    //-->
    </script>
    </head>
    
    <form name="form1">
    Name: <input type="text" size="50" name="message"> <br />
    Click the button to get the message: <input type="button" value="Go Get It!" onClick="goAjax()">
    </form>
    
    
    </body>
    </html>

  2. #2
    Join Date
    Jun 2007
    Posts
    667
    Quote Originally Posted by MichiKen View Post
    I'm new to AJAX. Trying to write a program that requests a file from a server and displays the text in the requested file. Not having any luck. Nothing happens when I click on the button.
    Something certainly does happen - in the error console that you're ignoring.

  3. #3
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    document.form1.message.write won't work...

Thread Information

Users Browsing this Thread

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

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