jQuery YQL SELECT FROM rss变量 [英] jQuery YQL SELECT FROM rss variable

查看:65
本文介绍了jQuery YQL SELECT FROM rss变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个变量"woeid",我想输入"w"的值-

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){

为什么不起作用?

整个脚本-

<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+"&deg;");
                        $('#weatherText').html(w.condition.text+"");
                        $('#geolat').html(w.title+"");

                        $('#var').html(lat+"latitude");

                    });

            });
         </script> 

推荐答案

问题出在数据检索的异步特性上.

The problem is with the asynchronous nature of your data retrieval.

发送第一个查询后,第二个YQL查询被立即发送出去.该第二个查询仅应在收到第一个查询的响应后进行,因为这为第二个查询提供了WOEID.

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.

简而言之,将第二个$.YQL(…)调用移至第一个的回调中.

In short, move the second $.YQL(…) call to within the callback of the first.

这是一个快速重构的示例, http://jsbin.com/oruhe6

Here's a quickly refactored example, http://jsbin.com/oruhe6

这篇关于jQuery YQL SELECT FROM rss变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆