【发布时间】:2017-11-05 07:24:38
【问题描述】:
我正在做一个项目,我需要使用 javascript 或 jQuery 获取存储在 XML 文件中的数据。我的脚本代码是
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
type: "POST", //I also tried POST method
url: "match.xml",
dataType: "xml",
success: function(data){
$(data).find("mchdata match").each(function(){ //match is a tag
$(".container").append('<div class="head">'+ $(this).attr("src") +'</div>'); //src is an attribute of match tag.
});
},
error: function(){
$(".container").text("Error occured");
}
});
});
</script>
html代码是
<body>
<div class="container"></div>
</body>
每次我运行此代码时,它都会在容器 div 中显示“发生错误”消息。我试图检查页面,它显示了一条错误消息,例如
加载文件失败:///C:/Users/vinay%20Dahiya/Desktop/match.xml: 跨域请求仅支持协议方案:http、 数据,铬,铬扩展,https。
我不知道这是什么意思。这是正确的脚本网址吗?
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
我在过去 2-3 天试图解决这个问题,但不知道代码中到底出了什么问题
XML 文件是View XML file
提前致谢。
【问题讨论】:
-
你不能用
file://URLs做Ajax。安装 HTTP 服务器。 -
它可以在本地主机上工作吗?
-
我在 Tomcat 服务器上使用它,但仍然无法正常工作
标签: javascript jquery html xml