jQuery YQL 从 rss 变量中选择

发布于 2024-10-30 16:55:45 字数 1548 浏览 0 评论 0原文

所以我有一个变量“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> 

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨后彩虹 2024-11-06 16:55:46

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

第二个 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文