已编辑
我需要用存储在外部 XML 文件中的内容填充网页。我找到了一个工作示例:http: //jsfiddle.net/9eqvq/
但是,在此示例中,数据直接写入 HTML 文档。
应用程序应该能够读取远程 XML 文件。我尝试使用 jQueryparseXML
方法执行此操作,但无法访问 XML 文档。
当我尝试在控制台中输出 XML 文档的全部内容时,如下面的代码所示,我在 Chrome 的控制台中看到以下错误:
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
这是我正在使用的代码:
<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>
当我打开index.html
(通过双击)包含上面的脚本时,我看到了成功消息
Message from Success function
但不是 XML 文档的数据。当我$("#output").text("Message from Success function");
用$("#output").text(data);
Safari 替换时,Chrome 和 Firefox 只显示我
[object XMLDocument]
有人可以告诉我我做错了什么吗?是否有不需要本地网络服务器的工作演示文件?顺便说一句,我还尝试在 XAMPP 和 Node.js 网络服务器上使用 Safari、Chrome、Firefox 运行代码——但没有成功。任何帮助,将不胜感激。