SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2010
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up Help me with Ajax???

    I want to get data from a sever.

    Ex. I have get data with result is:
    <input type="text" name="pg" value="hihihi"></a>
    when you run it with IE

    But, My code don't run with firefox, chrome:
    Help me ???
    Thanks all!

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
      <head>
      <title>Using XMLHttpRequest</title>
      <script type="text/javascript" src="1.js"></script>
    </head>
    
    <script>
    function getHTTPObject() {
      var xhr = false;
      if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
      } else if (window.ActiveXObject) {
        try {
          xhr = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
          try {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
          } catch(e) {
            xhr = false;
          }
        }
      }
      return xhr;
    }
    
    function grabFile(file) {
      var request = getHTTPObject();
      if (request) {
        request.onreadystatechange = function() {
          displayResponse(request);
        };
        request.open("GET", file, true);
        request.send(null);
      }
    }
    
    function displayResponse(request) {
      if (request.readyState == 4) {
        if (request.status == 200 || request.status == 304) {
          alert(request.responseText);
        }
      }
    }
    </script>
    
    
    <body>
      <p>
        <a href="http://muangay123.com/3.htm" onclick="grabFile(this.href); return false;">
    click
        </a>
    
      </p>
    </body>
    </html>
    Last edited by ralph.m; Jan 31, 2013 at 20:52. Reason: added code tags

  2. #2
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,208
    Mentioned
    456 Post(s)
    Tagged
    8 Thread(s)
    What are you expecting that code to do? It isn't really set up to do anything with the retrieved data. Normally you'd want to place it on the page once you've retrieved it. Anyhow, the code works to get the contents of the file. For me, in Chrome at least, I get an alert of the contents of 3.htm.

    Your script is in an odd location, though—wedged between the </head> and <body> tags. Preferably, move it to the bottom of the page, just before the </body> tag ... or at least put it in the <head> section.


Tags for this Thread

Bookmarks

Posting Permissions

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