我正在使用 Tumblr API 在我的网站上生成图像 Feed。
我可以将照片和网址确定,但不是字幕。
我制作了一个脚本,只需循环遍历帖子:
成功:function(results){
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];
但我没有得到任何结果 - 甚至没有任何错误。
谁会知道如何正确地做到这一点?
2 回答
线索在文档中
您正在尝试的代码与Photoset帖子的单张照片的标题有关。
var caption = results.response.posts[i].photos[0].caption
您的代码似乎建议您处理照片帖子而不是Photoset帖子,因此您将使用以下内容:
var caption = results.response.posts[i].caption
希望有所帮助。
首先,我会使用$.each()进行循环,因为你使用 JQuery:
如果您没有从 API 中检索期望的结果,请尝试 console.log(results);查看您在 Firebug 或其他 Web 检查器中使用的数据类型。