问题
So I have a variable "woeid" that I'm am trying to put in for the value of "w" -
$.YQL("select * from rss where url='http://weather.yahooapis.com/forecastrss?w="+woeid"'",function(data){
Why won't it work?
Edit: The whole script -
<script>
$(document).ready(function() {
$.YQL = function(query, callback) {
var encodedQuery = encodeURIComponent(query.toLowerCase()),
url = 'http://query.yahooapis.com/v1/public/yql?q='
+ encodedQuery + '&format=json&callback=?';
$.getJSON(url, callback);
};
$.YQL("select place.woeid from flickr.places where lat=34.45 and lon=-118.54", function(data) {
var w=data.query.results.places.place;
woeid = w.woeid
});
$.YQL("select * from rss where url='http://weather.yahooapis.com/forecastrss?w=" + woeid,function(data){
var w=data.query.results.item;
var class=w.condition.text;
var encodedclass = class.replace(/\s+/g, '-').toLowerCase();
$('body').addClass(encodedclass);
$('#weatherTemp').html(w.condition.temp+"°");
$('#weatherText').html(w.condition.text+"");
$('#geolat').html(w.title+"");
$('#var').html(lat+"latitude");
});
});
</script>
回答1:
The problem is with the asynchronous nature of your data retrieval.
The second YQL query is getting sent out immediately after sending the the first one. That second query should only be made after the response from the first one has been received, since that is what provides the WOEID for the second query.
In short, move the second $.YQL(…)
call to within the callback of the first.
Here's a quickly refactored example, http://jsbin.com/oruhe6
来源:https://stackoverflow.com/questions/5554804/jquery-yql-select-from-rss-variable