Edited
I need to fill a webpage with content stored in an external XML file. I found a working example: http://jsfiddle.net/9eqvq/
However, in this example the data is directly written into the HTML document.
The application should be able to read from a remote XML file.
I tried to do this with the jQuery parseXML
method, but I can't get access to the XML document.
When I'm trying to output the entire content of the XML document in the console as shown in the code below, I see the following error in Chrome's console:
XMLHttpRequest cannot load file://localhost/Users/Fabi/Documents/xml/xml.xml.
Cross origin requests are only supported for protocol schemes:
http, data, chrome-extension, https, chrome-extension-resource. VM71 jquery-1.10.1.js:8724
send VM71 jquery-1.10.1.js:8724
jQuery.extend.ajax VM71 jquery-1.10.1.js:8154
(anonymous function) VM72 index.html:19
fire VM71 jquery-1.10.1.js:3074
self.fireWith VM71 jquery-1.10.1.js:3186
jQuery.extend.ready VM71 jquery-1.10.1.js:433
completed VM71 jquery-1.10.1.js:104
Here's the code I'm using:
<html>
<head>
<title>Parsing XML File</title>
<script type="text/javascript" src="jquery-1.10.1.js"></script>
</head>
<body>
<div id="output">Default text without manpulation</div>
<script>
$(document).ready(function()
{
$.ajax({
url: "xml/xml.xml",
dataType: "xml",
success: function(data)
{
console.log(data);
$("#output").text("Message from Success function");
},
error:function()
{
$("#output").text("Message from Error function");
}
});
});
</script>
</body>
</html>
When I open index.html
(by double-clicking) which includes the script above, I see the the success essage
Message from Success function
but not the data of the XML document. When I replace $("#output").text("Message from Success function");
with $("#output").text(data);
Safari, Chrome and Firefox only show me
[object XMLDocument]
Can someone tell me what I'm doing wrong? Are there maybe working demo files which do not require a local webserver? By the way I also tried to run the code with Safari, Chrome, Firefox on a XAMPP and on a Node.js webserver – without success. Any help would be appreciated.