【问题标题】:jQuery YQL SELECT FROM rss variablejQuery YQL 从 rss 变量中选择
【发布时间】:2011-07-30 02:45:08
【问题描述】:

所以我有一个变量“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+"&deg;");
                        $('#weatherText').html(w.condition.text+"");
                        $('#geolat').html(w.title+"");

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

                    });

            });
         </script> 

【问题讨论】:

  • 什么不起作用?你得到什么错误?

标签: jquery variables rss quotes yql


【解决方案1】:

问题在于数据检索的异步性质。

第二个 YQL 查询在发送第一个查询后立即发送出去。第二个查询只能在收到第一个查询的响应后进行,因为这是为第二个查询提供 WOEID 的原因。

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

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

【讨论】:

    猜你喜欢
    • 2012-03-21
    • 1970-01-01
    • 2013-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-05
    • 2016-07-22
    • 2013-07-26
    相关资源
    最近更新 更多