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
    New Coder
    Join Date
    Sep 2010
    Location
    The Twilight Zone
    Posts
    86
    Thanks
    17
    Thanked 2 Times in 2 Posts

    xmlHttpRequest Object not retrieving XML File

    Hey everyone,

    I'm testing the code for ajax by trying to retrieve a simple xml document. I placed both the xml file and the html file on my desktop, but it's not working. I don't think there's anything wrong with the code- I found it on w3schools and modified it to retrieve the new file I created. Please take a look.

    HTML FILE
    Code:
    <!DOCTYPE html PUBLIC "-//W3C
    //DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/
    xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
    	<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
    	<title>Insert Title Here</title>
    <script language="JavaScript" type="text/javascript">
    <!--
    function loadXMLDoc()
    {
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","info.xml",true);
    xmlhttp.send();
    }
    //-->
    </script>
    
    <style type="text/css">
    </style>
    
    </head>
    <body>
    
    <div id="myDiv"><h2>I will attempt to change this text using AJAX!</h2></div>
    <button type="button" onClick="loadXMLDoc()">Change Content PLZ</button>
    
    
    </body>
    </html>
    XML File
    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <note>
    <message>Hello World!</message>
    </note>

    Thanks!

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    HTTP requests need a webserver as a destination, so without a webserver (even on localhost) it is not possible to retrieve files.

  • Users who have thanked devnull69 for this post:

    Ahlahn (01-11-2011)

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Quote Originally Posted by devnull69 View Post
    HTTP requests need a webserver as a destination, so without a webserver (even on localhost) it is not possible to retrieve files.
    Ihave the following code in an html file
    on my desktop (windows xp)...

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    	<title>An XHTML 1.0 Strict standard template</title>
    	<meta http-equiv="content-type" 
    		content="text/html;charset=utf-8" />
    <script type="text/javascript">
    //<![CDATA[
    var xmlhttp=false;
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
    // JScript gives us Conditional compilation, we can cope with old IE versions.
    // and security blocked creation of the objects.
     try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
      try {
       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
    alert('!IE')
       xmlhttp = false;
      }
     }
    @end @*/
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    	try {
    		xmlhttp = new XMLHttpRequest();
    	} catch (e) {
    		xmlhttp=false;
    	}
    }
    if (!xmlhttp && window.createRequest) {
    	try {
    		xmlhttp = window.createRequest();
    	} catch (e) {
    		xmlhttp=false;
    	}
    }
    xmlhttp.open("GET","test.xml" ,true);
     xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState==4) {
       alert(xmlhttp.getAllResponseHeaders())
    alert(xmlhttp.responseXML)
    alert(xmlhttp.responseText)
    
    var root = xmlhttp.responseXML.getElementsByTagName('*');
    alert(root.length)
      }
     }
     xmlhttp.send(null)
    //]]>
    </script>
    </head>
    <body>
         <p>… Your HTML content here …</p>
    </body>
    </html>
    also on my desktop i have an xml file
    named test.xml

    try it out
    works for me

  • Users who have thanked DaveyErwin for this post:

    devnull69 (01-11-2011)

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Wow ok so the problem must be somewhere else ... I keep learning every day

    @DaveyErwin: Are you able to detect the problem then?

  • #5
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    there is a lot of bad ajax around
    even at w3school
    try like this ...

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="content-type" 
    		content="text/html;charset=utf-8" />
    	<title>An XHTML 1.0 Strict standard template</title>
    	<meta http-equiv="content-type" 
    		content="text/html;charset=utf-8" />
    <script type="text/javascript">
    //<![CDATA[
    var xmlhttp=false;
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
    // JScript gives us Conditional compilation, we can cope with old IE versions.
    // and security blocked creation of the objects.
     try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
      try {
       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
    alert('!IE')
       xmlhttp = false;
      }
     }
    @end @*/
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    	try {
    		xmlhttp = new XMLHttpRequest();
    	} catch (e) {
    		xmlhttp=false;
    	}
    }
    if (!xmlhttp && window.createRequest) {
    	try {
    		xmlhttp = window.createRequest();
    	} catch (e) {
    		xmlhttp=false;
    	}
    }
    xmlhttp.open("GET","test.xml" ,true);
     xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState==4) {
       document.getElementById('dsp').innerHTML=xmlhttp.responseText;
      }
     } 
    //]]>
    </script>
    </head>
    <body>
         <p id="dsp">… This text will change …</p>
    <p><input type="button" onclick="xmlhttp.send(null)"/></p>
    </body>
    </html>
    keyword:ajax;
    keyword:Msxml2.XMLHTTP;
    keyword:Microsoft.XMLHTTP;
    keyword:XMLHttpRequest;
    keyword:read from file system;
    Last edited by DaveyErwin; 01-11-2011 at 08:17 PM.


  •  

    Posting Permissions

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