javascript中WOEID(使用YQL或rss)的天气预报

Weather forecast from WOEID (using YQL or rss) in javascript?

本文关键字:rss 天气预报 YQL WOEID 使用 javascript      更新时间:2023-09-26

有什么方法可以从javascript中的woeid获取天气预报吗?

我试着使用雅虎的rss提要,但无法使用。这是我的代码

    var url = "http://weather.yahooapis.com/forecastrss?w=" + encodeURIComponent('WOEID here');
    $.ajax({
        url: url,
        dataType: 'jsonp',
        jsonpCallback: function(data) { console.log(data); },
        success: function(data) { alert("success"); }
    });

有什么建议吗?

以下是使用jQuery和YQL:获得所需信息的最简单方法

var woeid = "26355493";
var $url = "http://query.yahooapis.com/v1/public/yql?callback=?";
$.getJSON($url, {
    q: "select * from xml where url=" +
       "'"http://weather.yahooapis.com/forecastrss?w=" + woeid + "'"",
    format: "json"
  }, function (data) {
    console.log(data.query.results.rss.channel);
  }
);​

YQL控制台中的查询。。。

jsfiddle中的JavaScript代码。。。

显然,Weather API以RSS格式返回其结果,而您的函数期望它们以jsonp格式返回。考虑使用雅虎!管道为您获取天气RSS提要,对其进行处理,并以jsonp格式返回。

这是一个做类似事情的管道:

http://pipes.yahoo.com/pipes/pipe.info?_id=4d160cd8ed9d6d78164213928a51507d

正如dragon所建议的,我创建了一个Yahoo Pipe——这是我的完整代码;代码中的url是我创建的Yahoo Pipe。

$(function(){
   var url = "http://pipes.yahoo.com/pipes/pipe.run?_id=e33143abd20b19a0173b3a4b479fa4d3&_render=json&w=YOURWOEIDHERE";
   function createRequest() {
       try { return new XMLHttpRequest(); } catch(e) {}
       try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
       return null;
   }
   var request = createRequest();
   request.open("GET", url, true);
   request.onreadystatechange = callback;
   request.send(null);
   function callback() {
       if(request.readyState != 4) { return }
       Obj = $.parseJSON(request.responseText);
       console.log(Obj);
   }
});

引用:
雅虎管道:http://pipes.yahoo.com/pipes/pipe.info?_id=e33143abd20b19a0173b3a4b479fa4d3
jQuery 1.5-JSON错误标签无效