jQuery YQL 从 rss 变量中选择
所以我有一个变量“woeid”,我试图将其放入“w”的值中 -
$.YQL("select * from rss where url='http://weather.yahooapis.com/forecastrss?w="+woeid"'",function(data){
为什么它不起作用?
编辑:整个脚本 -
<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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于数据检索的异步性质。
第二个 YQL 查询在发送第一个 YQL 查询后立即发送出去。第二个查询只能在收到第一个查询的响应后进行,因为这是为第二个查询提供 WOEID 的原因。
简而言之,将第二个
$.YQL(…)
调用移至第一个回调内。这是一个快速重构的示例,http://jsbin.com/oruhe6
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