Reputation: 2441
Is there any way of getting the weather forecast from woeid in javascript?
I tried using rss feed of yahoo, but couldn't get it to work. here is my code
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"); }
});
Any suggestions?
Upvotes: 2
Views: 3635
Reputation: 17023
Here's the simplest way to get the info you want using jQuery and 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);
}
);
The query in the YQL console...
The JavaScript code in jsfiddle...
Upvotes: 2
Reputation: 2441
As dragon suggested, I created a Yahoo Pipe - here is my complete code; the url in the code is the Yahoo Pipe I created.
$(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);
}
});
references:
Yahoo Pipe: http://pipes.yahoo.com/pipes/pipe.info?_id=e33143abd20b19a0173b3a4b479fa4d3
jQuery 1.5 - JSON error invalid label
Upvotes: 0
Reputation: 1785
Apparently, the Weather API returns its results in RSS
format, while your function is expecting them in jsonp
format. Consider using Yahoo! Pipes to fetch the weather RSS
feed for you, process it, and return it in jsonp
format.
Here's a pipe that does something similar:
http://pipes.yahoo.com/pipes/pipe.info?_id=4d160cd8ed9d6d78164213928a51507d
Upvotes: 1