【发布时间】:2011-10-03 15:18:27
【问题描述】:
所以,我编写了一些 JavaScript 从我的桌面抓取一个 xml 文件并将其显示在一个 html 页面上。但是,我现在已将我的 xml 文件添加到网络服务器(猫鼬)。我想从该服务器调用文件,但是每当我从服务器调用文件时它都不起作用,但是当我从桌面调用它时它加载正常。
我想换
xmlhttp.open("GET","Devices.xml",false);
与
xmlhttp.open("GET","http://localhost:8080/Devices.xml",false);
这里是代码
<html>
<head>
<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","Devices.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
// the <Device> list
x = xmlDoc.getElementsByTagName('Device');
// make a function that extracts the attributes out of a Node
function getDeviceAttributes(dvc) {
var name = dvc.getAttribute("name");
var uuid = dvc.getAttribute("uuid");
var id = dvc.getAttribute("id");
return "<p>name: " + name + "<br> uuid: " + uuid + "<br> id: "+ id + "</p>";
}
// loop through the list
// assuming order doesn’t matter
var txt = '';
for (var i = x.length; i--;) {
txt += getDeviceAttributes(x[i]);
}
//show the result on page load
window.onload = function() {
document.getElementById("showDevices").innerHTML = txt;
};
</script>
</head>
<body>
<div id='showDevices'></div>
</body>
</html>
有人知道我怎样才能让它工作吗?
有人告诉我使用 AJAX 和 Jquery,但我不知道如何开始,甚至不知道从哪里开始。
【问题讨论】:
-
您是否提供从同一网络服务器获取该文件的页面?如果不是,您可能会遇到一些同源问题
标签: javascript jquery html xml ajax