Reputation: 31
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. :(
Upvotes: 0
Views: 5661
Reputation: 21
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.
Upvotes: 2
Reputation: 8421
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
Upvotes: 2