Yahoo forecastjson gives XML error

江枫思渺然 提交于 2019-12-12 07:03:40

问题


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

When I run:

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

I get the following error:

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

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?

Thanks


回答1:


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

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

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

here is an example,

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.

DEMO



来源:https://stackoverflow.com/questions/10375946/yahoo-forecastjson-gives-xml-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!