【发布时间】:2015-05-09 08:32:25
【问题描述】:
如果这是一个非常基本的问题,我很抱歉,但我真的被困在这里,我没有太多时间。 因此,我创建了一个包含数据的 xml 文件,并希望使用 javascript 在 html 中显示它,但我得到的只是一个空白页。这是我的 xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<text>
<content> a random text </content>
</text>
这是我的 html 文件 xml
<script>
document.write("begin");
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","text.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
var x=xmlDoc.getElementsByTagName("text");
document.write(x[0].getElementsByTagName("content") [0].childNodes[0].nodeValue);
</script>
</body>
</html>
也许它使用 httpRequest 所以它需要一个服务器,但是我尝试了 apache 并且它不起作用。
编辑:我在 chrome 中打开控制台,它说错误在xmlhttp.send(); 行中,错误说“跨源请求仅支持协议方案:http、data、chrome、chrome-extension、https、chrome -扩展资源。”
编辑:它在 mozilla 上有效,但在 chrome 中无效
【问题讨论】:
标签: javascript html xml load