在 getJASON 回调函数的 DATA 中使用变量

发布于 2024-09-04 16:19:39 字数 1001 浏览 2 评论 0原文

我的问题是管理获取标签并使用变量的代码(var searchterm= ??????)。使用 JSON,我想首先使用 tagthe 获取“位置”标签,并显示来自 flickr 的相关照片。

<!DOCTYPE html>
<html>
<head>
  <style>img{ height: 100px; float: left; }</style>
  <script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
 <div id="images">

</div>
<script>
$.getJSON("http://tagthe.net/api/?url=http://www.knallgrau.at/en&view=json&callback=MyFunc",function(data){         
              var searchterm=data[location];
        });


$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags="+searchterm+"&tagmode=any&format=json&jsoncallback=?",
        function(data){
          $.each(data.items, function(i,item){
            $("<img/>").attr("src", item.media.m).appendTo("#images");
            if ( i == 3 ) return false;
          });
        });</script>
</body>
</html> 

My problem is manage the code which get the tag and use is as variable (var searchterm= ??????). With JSON I want first get the "location" tags with tagthe and show the relate photos from flickr.

<!DOCTYPE html>
<html>
<head>
  <style>img{ height: 100px; float: left; }</style>
  <script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
 <div id="images">

</div>
<script>
$.getJSON("http://tagthe.net/api/?url=http://www.knallgrau.at/en&view=json&callback=MyFunc",function(data){         
              var searchterm=data[location];
        });


$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags="+searchterm+"&tagmode=any&format=json&jsoncallback=?",
        function(data){
          $.each(data.items, function(i,item){
            $("<img/>").attr("src", item.media.m).appendTo("#images");
            if ( i == 3 ) return false;
          });
        });</script>
</body>
</html> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

迷雾森÷林ヴ 2024-09-11 16:19:39

第二个 $.getJson() 请求在第一个请求完成之前运行,并且您在第一个请求中创建的变量无论如何都超出了第二个请求的范围。

将第二个 $.getJson() 请求放在第一个请求的回调中,以便在第一个请求完成之前它不会运行。

$.getJSON("http://tagthe.net/api/?url=http://www.knallgrau.at/en&view=json&callback=MyFunc",function(data){         
    var searchterm=data[location];

    $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags="+searchterm+"&tagmode=any&format=json&jsoncallback=?",
        function(data){
          $.each(data.items, function(i,item){
            $("<img/>").attr("src", item.media.m).appendTo("#images");
            if ( i == 3 ) return false;
          });
        });
});

编辑:

这是第一次调用使用 $.ajax() 的版本。我直接指定了 jsonp ,并去掉了 URL 中的回调。

查看实例: http://jsfiddle.net/uGJYr/2/ (更新)

$.ajax({
    url:"http://tagthe.net/api/?url=http://www.knallgrau.at/en&view=json",
    dataType:'jsonp',
    success:function(data){

            // You needed to dig deeper to get the location
         var searchterm=data.memes[0].dimensions.location[0];

         $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags="+searchterm+"&tagmode=any&format=json&jsoncallback=?",
          function(data){
              $.each(data.items, function(i,item){
                  $("<img/>").attr("src", item.media.m).appendTo("#images");
                  if ( i == 3 ) return false;
              });
          });
    }
});

编辑:

我更改了上面的 var searchterm = 行,因为返回的数据对象比建议的要复杂得多在你的原始代码中。

您需要:

var searchterm=data.memes[0].dimensions.location[0];

...因为从第一个请求返回的数据如下所示:

{"memes":[
            {   "source":"http://www.knallgrau.at/en",
                "updated":"Tue Jun 08 19:29:51 CEST 2010",
                "dimensions":{  "topic":["content","knallgrau","overview","agency","nullItem","Deutsch","foundation","Company","management","English"],
                                "content-type":["text/html"],
                                "author":["vi knallgrau"],
                                "person":["dieter rappold","Dieter Rappold"],
                                "title":["Company - vi knallgrau"],
                                "location":["Austria","Vienna"],
                                "language":["english"],
                                "size":["5494"]
                            }
            }
        ]
}​

The second $.getJson() request is running before the first is complete, and the variable you created in the first is out of scope of the second anyway.

Place the second $.getJson() request inside the callback for the first so that it doesn't run until the first is complete.

$.getJSON("http://tagthe.net/api/?url=http://www.knallgrau.at/en&view=json&callback=MyFunc",function(data){         
    var searchterm=data[location];

    $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags="+searchterm+"&tagmode=any&format=json&jsoncallback=?",
        function(data){
          $.each(data.items, function(i,item){
            $("<img/>").attr("src", item.media.m).appendTo("#images");
            if ( i == 3 ) return false;
          });
        });
});

EDIT:

Here's a version that uses $.ajax() for the first call. I specified jsonp directly, and got rid of the callback in the URL.

Check out the live example: http://jsfiddle.net/uGJYr/2/ (updated)

$.ajax({
    url:"http://tagthe.net/api/?url=http://www.knallgrau.at/en&view=json",
    dataType:'jsonp',
    success:function(data){

            // You needed to dig deeper to get the location
         var searchterm=data.memes[0].dimensions.location[0];

         $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags="+searchterm+"&tagmode=any&format=json&jsoncallback=?",
          function(data){
              $.each(data.items, function(i,item){
                  $("<img/>").attr("src", item.media.m).appendTo("#images");
                  if ( i == 3 ) return false;
              });
          });
    }
});

EDIT:

I changed the var searchterm = line above, as the data object returned is much more complex that suggested in your original code.

You needed:

var searchterm=data.memes[0].dimensions.location[0];

...because the data returned from the 1st request looks like this:

{"memes":[
            {   "source":"http://www.knallgrau.at/en",
                "updated":"Tue Jun 08 19:29:51 CEST 2010",
                "dimensions":{  "topic":["content","knallgrau","overview","agency","nullItem","Deutsch","foundation","Company","management","English"],
                                "content-type":["text/html"],
                                "author":["vi knallgrau"],
                                "person":["dieter rappold","Dieter Rappold"],
                                "title":["Company - vi knallgrau"],
                                "location":["Austria","Vienna"],
                                "language":["english"],
                                "size":["5494"]
                            }
            }
        ]
}​
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文