【发布时间】:2011-12-24 09:06:02
【问题描述】:
当我尝试使用简单的 javascript 解析本地 xml 文件时,xhr.open 之后的代码没有被执行。
<html>
<head>
<script type="text/javascript">
function createXMLHttpRequestObject()
{
if (window.XMLHttpRequest) {
xhttp=new XMLHttpRequest();
} else {
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
return xhttp;
}
function makeRequest()
{
var xhr = createXMLHttpRequestObject();
try {
xhr.open("GET", 'books.xml', false);
xhr.send(null);
//xhr.onreadystatechange = function() {
//if(this.readyState == 4) {
xmlDoc=xhr.responseXML;
alert(xmlDoc);
//}
}
} catch( err ) {
alert("ERROR: " + err.description);
}
}
</script>
</head>
<body>
<div onclick="makeRequest();">test<br></div>
<div id="out">Output Here</div>
</body>
</html>
books.xml 文件如下,
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
在互联网上搜索时,我发现“出于安全原因,现代浏览器不允许跨域访问。” http://www.w3schools.com/xml/xml_parser.asp
请帮忙。
【问题讨论】:
标签: javascript xml xmlhttprequest xml-parsing