jQuery中的XML解析对我似乎不起作用 [英] XML parsing in jquery doesn't seem to work for me
问题描述
我有一个静态html页面weather.html
I have a static html page weather.html
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
type: "GET",
url: "http://www.google.com/ig/api?weather=Delhi",
dataType: "xml",
success: parseXml
});
});
function parseXml(xml) {
$(xml).find("weather").each(function() {
alert($(this).attr("temp_c"));
});
}
</script>
</head>
<body>
</body>
</html>
当我检查槽中的萤火虫时,似乎没有在页面中显示警报,
The alert doesn't seem to get displayed in the page when i inspected trough firebug i found this,
XML Parsing Error: no element found Location: moz-nullprincipal:{08ba4230-2feb-48d3-969e-b53579b07b52} Line Number 1, Column 1:
^
还有功能parseXml
似乎没有被调用...
also function parseXml
doesn't seem to get called...
推荐答案
您无法使用AJAX访问远程域.请参见相同来源政策.您正在尝试访问位于google.com
上的脚本,因此,除非此页面位于同一域中,否则它将无法正常工作.进行此工作的唯一方法是在服务器上设置代理服务器脚本,您将向该服务器发送AJAX调用,并将该调用委托给google.com
.另一种选择是使用 JSONP ,但是遥远的脚本需要支持它.
You cannot access distant domains using AJAX. See Same Origin Policy. You are trying to access a script located on google.com
, so unless this is page is on hosted on this same domain it won't work. The only way to make this work is to setup a proxy server script on your server to which you will send the AJAX call and it will delegate the call to google.com
. Another alternative is to use JSONP but the distant script needs to support it.
这篇关于jQuery中的XML解析对我似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!