SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict AdRock952's Avatar
    Join Date
    Aug 2006
    Posts
    243
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    w3schools ajax example not working

    I got an example from www.w3schools.com and can't get it to work with any browser

    I copied the code exactly and got their example of an xml file but it doesn't work like it does on the site

    I get this alert message when i click the button. Any ideas why it's not working?
    Code:
    <html>
    <head>
    <script type="text/javascript">
    var xmlhttp;
    function loadXMLDoc(url)
    {
    xmlhttp=null;
    if (window.XMLHttpRequest)
      {// code for IE7, Firefox, Opera, etc.
      xmlhttp=new XMLHttpRequest();
      }
    else if (window.ActiveXObject)
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    if (xmlhttp!=null)
      {
      xmlhttp.onreadystatechange=state_Change;
      xmlhttp.open("GET",url,true);
      xmlhttp.send(null);
      }
    else
      {
      alert("Your browser does not support XMLHTTP.");
      }
    }
    
    function state_Change()
    {
    if (xmlhttp.readyState==4)
      {// 4 = "loaded"
      if (xmlhttp.status==200)
        {// 200 = "OK"
        document.getElementById('A1').innerHTML=xmlhttp.status;
        document.getElementById('A2').innerHTML=xmlhttp.statusText;
        document.getElementById('A3').innerHTML=xmlhttp.responseText;
        }
      else
        {
        alert("Problem retrieving XML data:" + xmlhttp.statusText);
        }
      }
    }
    </script>
    </head>
    
    <body>
    <h2>Using the HttpRequest Object</h2>
    
    <p><b>Status:</b>
    <span id="A1"></span>
    </p>
    
    <p><b>Status text:</b>
    <span id="A2"></span>
    </p>
    
    <p><b>Response:</b>
    <br /><span id="A3"></span>
    </p>
    
    <button onclick="loadXMLDoc('note.xml')">Get XML</button>
    
    </body>
    </html>

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,683
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    If you're running the code locally then there is no server to provide a status of 200. Try uploading the code somewhere and giving it a go that way.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Or get yourself a package to run locally, like XAMPP.


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
  •