雅虎ForecastJSON给XML错误 [英] Yahoo forecastjson gives XML error

查看:134
本文介绍了雅虎ForecastJSON给XML错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

浏览该网站后,我发现您可以使用Forecastjson以JSON格式获取Yahoo天气.

After browsing through this site, I found that you can get Yahoo weather in a JSON format using forecastjson.

当我跑步时:

$.getJSON("http://weather.yahooapis.com/forecastjson?w=2112762724", function(data){
    ...
});

我收到以下错误:

XMLHttpRequest cannot load http://weather.yahooapis.com/forecastjson?w=2112762724. Origin null is not allowed by Access-Control-Allow-Origin.

我之前遇到过此错误,但通常是因为我正在尝试跨域加载XML,但这显然是JSON.如果转到getJSON函数中的链接,它将显示JSON数据.有人知道我为什么收到此错误吗?

I've gotten this error before but its normally because I'm trying to load XML cross domain but this is clearly JSON. If you go to the link in the getJSON function, it shows JSON data. Does anyone know why I am getting this error?

谢谢

推荐答案

使用JSON并不意味着您不会遇到跨域问题.那是一个对象标准.

Using JSON doesn't mean that you will not encounter cross domain problem. That is an object standard.

如果要发出跨域请求,则应使用 JSONP .

If you want to make a cross domain request, you should use JSONP.

您尝试请求的网址不支持JSONP请求.但是您可以使用YQL代替.

The url that you are trying to request, doesn't support JSONP request. But you can use YQL instead of that.

这是一个例子,

var query = escape('select item from weather.forecast where woeid="2295424"');
var url = "http://query.yahooapis.com/v1/public/yql?q=" + query + "&format=json&callback=c"; 

$.getJSON(url, function(data) {
   console.log(data);
});​

这是

And here is the URL that you can check json result.

演示

这篇关于雅虎ForecastJSON给XML错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆