【问题标题】:Tumblr API not retrieving photo captionsTumblr API 不检索照片标题
【发布时间】:2013-10-04 00:21:10
【问题描述】:

我正在使用 Tumblr API 在我的网站上生成图片。

我可以获取照片及其 URL,但不能获取标题。

我制作了一个脚本,它可以像这样简单地遍历帖子:

成功:函数(结果){

var i = 0;

 while (i < results.response.posts.length) {

    if (type == "photo") {
     var photourl = results.response.posts[i].photos[0].alt_sizes[0].url;
     var caption = results.response.posts[i].caption;

     $("#tumnews #newscara").append("<li><div class='tumpost'><a href='" + link + "'><img src='" + photourl + "' alt='" + title + "'/><div class='tumcaption'>" + caption + "</div></a></div></li>");
   }

  i++;
 }//END WHILE

但我无法检索字幕的数据,即使文档说它只是使用术语“字幕”(http://www.tumblr.com/docs/en/api/v2#photo-posts) 检索。

我也试过了:

  var caption = results.response.posts[i].photos[0].caption;

  var caption = results.response.posts[i]photos[0].caption[0];

但我没有得到任何结果 - 甚至没有任何错误。

有人知道如何正确地做到这一点吗?

【问题讨论】:

    标签: jquery ajax json api tumblr


    【解决方案1】:

    线索在文档中

    具有以下属性的照片对象:标题 - 字符串:用户提供的标题 对于单张照片(仅限照片集)

    您尝试的代码与 Photoset 帖子的单张照片的标题有关。

    var caption = results.response.posts[i].photos[0].caption

    您的代码似乎建议您处理 Photo 帖子而不是 Photoset 帖子,因此您可以使用以下代码:

    var caption = results.response.posts[i].caption

    希望对您有所帮助。

    【讨论】:

      【解决方案2】:

      首先,我会使用 $.each() 进行循环,因为您使用的是 JQuery:

      success: function(results){
        console.log(results); // <-- This is your best friend
      
        $.each(results.response.posts, function(k, post){
          if (type == "photo") {
           var photourl = post.photos[0].alt_sizes[0].url;
           var caption = post.caption;
           $("#tumnews #newscara").append("<li><div class='tumpost'><a href='" + link + "'><img src='" + photourl + "' alt='" + title + "'/><div class='tumcaption'>" + caption + "</div></a></div></li>");
          }
        });
      
      }
      

      如果您没有从 API 检索到预期结果,请尝试 console.log(results);查看您在 Firebug 或其他 Web 检查器中处理的数据类型。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-10-20
        • 2011-08-29
        • 2023-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多