使用 javascript 进行跨站点 XML 访问,无需服务器访问
我之前问了一个问题,但措辞不正确,所以被忽略了。
基本上,我正在创建一个 Windows 7 小工具,它将访问 www.weather.gov 中的 XML 数据并显示一些当前状况。
function weat()
{
var url = "http://www.weather.gov/xml/current_obs/KMSY.xml";
source.open("GET", url, false);
source.send(null);
info = source.responseXML;
document.write("<table border='1'>");
var stuff = info.getElementsByTagName("temp_f");
document.write("<tr><td>");
document.write(stuff);
document.write("</tr></td>");
document.write("</table>");
}
基本上我试图让它在我的桌面上工作,但我不完全确定问题是什么。我仍然认为这是一个跨站点问题,因为东西打印为未定义,但我不完全确定这一点。如果有人可以帮助我,我已经在谷歌上搜索可能的解决方案大约 6 个小时了,我感到非常沮丧。
I asked a question earlier but did not word it correctly so it got brushed over.
Basically, I am creating a windows 7 gadget that will access XML data from www.weather.gov and display some current conditions.
function weat()
{
var url = "http://www.weather.gov/xml/current_obs/KMSY.xml";
source.open("GET", url, false);
source.send(null);
info = source.responseXML;
document.write("<table border='1'>");
var stuff = info.getElementsByTagName("temp_f");
document.write("<tr><td>");
document.write(stuff);
document.write("</tr></td>");
document.write("</table>");
}
Basically I am trying to get this to work from my desktop, and I'm not totally sure what the problem is. I still think it is a cross-site problem, since stuff prints as undefined, but I'm not totally sure of that. If anyone could help me, I've been googling possible solutions for about 6 hours now and I'm getting pretty frustrated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您要么需要服务器端代理,要么需要找到支持 JSONP 的天气服务
You either need a server-side proxy or you need to find a weather service that supports JSONP
我对小工具不熟悉。但在桌面浏览器中,恐怕同源策略会阻止任何跨域访问。
一个例外是,在 Chromium 中,只有在命令行中启动 Chromium 时设置了 --disable-web-securities,本地页面 (file://) 才能访问远程 URL。
I'm not familiar with Gadget. But in desktop browsers, I'm afraid Same origin policy prevent from any cross domain access.
One exception is that in Chromium, local pages (file://) can access remote URL only if --disable-web-securities is set when starting Chromium in command line.