0

I copied this code from W3Schools (along with the original XML file cd_catalog.xml) and I'm getting a blank page:

<html>
<body>

<script type="text/javascript">
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("GET","cd_catalog.xml",false);
    xmlhttp.send();
    xmlDoc=xmlhttp.responseXML;

    document.write("<table border='1'>");
    var x=xmlDoc.getElementsByTagName("CD");
    for (i=0;i<x.length;i++)
    {
        document.write("<tr><td>");
        document.write(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);
        document.write("</td><td>");
        document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
        document.write("</td></tr>");
    }
    document.write("</table>");
</script>

</body>
</html>

I tried Opera, Firefox, IE and Chrome. Nothing. :(

CD Smith
  • 6,597
  • 7
  • 40
  • 66
pupul07
  • 31
  • 2
  • 3
  • 1
    How are you serving this? I am thinking your cd_catalog.xml can't be found... – Guy Sirton May 30 '12 at 05:05
  • 1
    Are you using a webserver, or accessing it via a file URL like file:///C:/test/test.html. The latter might be a problem. – McGarnagle May 30 '12 at 05:08
  • What do you get if you try `console.log(xmlhttp.responseXML)`? – nnnnnn May 30 '12 at 05:22
  • I am not using a web server. cd_catalog.xml is located in C:/ (the same directory where the cd_catalog.html file is located). I tried console.log(xmlhttp.responseXML) but I still get a blank screen. :( – pupul07 May 30 '12 at 06:45

2 Answers2

2

Since you're not using a web server try doing this:

xmlhttp.open("GET","file:///C:/cd_catalog.xml", false);

You may end up needing a web server because the browsers will not allow your script access to the local files, e.g. see discussion here: http://www.webdeveloper.com/forum/showthread.php?t=233306

So consider setting up a simple web server on your machine, like lighttpd.

EDIT: the way I interpret the spec ( http://dvcs.w3.org/hg/xhr/raw-file/tip/Overview.html#the-open%28%29-method ) is that this must be done through HTTP. It's not absolutely clear though. A file: URL may not be allowed in this context so the expectation is that the code snippet above may not work and you will need a web server.

A related question is:

Read file:// URLs in IE XMLHttpRequest

and

Allow Google Chrome to use XMLHttpRequest to load a URL from a local file

Community
  • 1
  • 1
Guy Sirton
  • 8,331
  • 2
  • 26
  • 36
  • Tried the file thing, didn't work in either browser. This is the error message I'm getting (Chrome console): 1)XMLHttpRequest cannot load file:///C:/cd_catalog.xml. Cross origin requests are only supported for HTTP. 2)Uncaught Error: NETWORK_ERR: XMLHttpRequest Exception 101 cd_catalog.html:14 Anyways, I guess I'll try running it from WAMP or something. – pupul07 May 31 '12 at 17:21
  • There are lots of options for web servers that you can set up pretty easily. Looks like you need one. Also see here: http://stackoverflow.com/questions/8449716/cross-origin-requests-are-only-supported-for-http-but-its-not-cross-domain – Guy Sirton May 31 '12 at 20:23
2

Upload your code to a web server. You will get the desired output on all browsers such as IE, Chrome, or Mozilla. But if you try this same code on the local machine, then IE and Chrome won't work.

ErikE
  • 48,881
  • 23
  • 151
  • 196