jQuery YQL SELECT FROM rss variable

大兔子大兔子 提交于 2020-01-14 03:34:28

问题


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+"&deg;");
                        $('#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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!