使用 tumblr API(JSON) 和 $.getJSON 从我的 tumblr 博客中获取最新文章
Using tumblr API (JSON) and $.getJSON to grab the most recent posts from my tumblr blog
>我无法从格式化为 JSON 的 tumblr 中提取内容。我从tumblr注册了一个API密钥,并有正确的API链接,但在$.getJSON中缺少一些东西。下面我将展示我的JSON和jQuery的外观。
这是我的 JSON 的样子:
{
"meta": {
"status": 200,
"msg": "OK"
},
"response": {
"blog": {
"title": "BLOG TITLE",
"name": "BLOG NAME",
"posts": 96,
"url": "http://BLOGTITLE.tumblr.com/",
"updated": 1370605005,
"description": "BLOG DESCRIPTION",
"ask": false,
"ask_anon": false,
"is_nsfw": false,
"share_likes": true,
"likes": 0
},
"posts": [
{
"blog_name": "BLOG NAME",
"id": 52373434879,
"post_url": "POST URL",
"slug": "POST SLUG",
"type": "photo",
"date": "2013-03-07 11:36:44 GMT",
"timestamp": 1370605004,
"state": "published",
"format": "html",
"reblog_key": "T0m0e51u",
"tags": [],
"short_url": "SHORT URL",
"highlighted": [],
"note_count": 1,
"caption": "PHOTO CAPTION TEXT DESCRIPTION",
"image_permalink": "http://BLOGTITLE.tumblr.com/image/52373434879",
"photos": [
{
"caption": "",
"alt_sizes": [
{
"width": 640,
"height": 960,
"url": "http://31.media.tumblr.com/.../...jpg"
},
{
"width": 500,
"height": 750,
"url": "http://31.media.tumblr.com/.../..._500.jpg"
},
{
"width": 400,
"height": 600,
"url": "http://24.media.tumblr.com/.../..._400.jpg"
},
{
"width": 250,
"height": 375,
"url": "http://31.media.tumblr.com/.../..._250.jpg"
},
{
"width": 100,
"height": 150,
"url": "http://24.media.tumblr.com/.../..._100.jpg"
},
{
"width": 75,
"height": 75,
"url": "http://24.media.tumblr.com/.../..._75sq.jpg"
}
],
"original_size": {
"width": 640,
"height": 960,
"url": "http://31.media.tumblr.com/.../..._1280.jpg"
}
}
]
},
这是我的jQuery的样子:
jQuery(document).ready(function(){
var url = "http://api.tumblr.com/v2/blog/BLOGNAME.tumblr.com/posts?api_key=APIKEY&limit=5";
var src;
var caption;
$.getJSON(url, function(results){
$.each(results.response, function(i,item){
src = "posts.photos.alt_sizes.url";
caption = posts.caption;
$("<img/>").attr("src", src).appendTo("#submissions").wrap('<div class="postImage"></div>').after('<span class="postCaption">' + caption + '</div>');
});
.HTML:
<div id="submissions"></div>
我很确定问题出在 $.getJSON 或 $.each 中,但我无法弄清楚到底是什么。有什么帮助吗?
谢谢。
您可能会
收到错误,因为您正在尝试访问未定义的变量post
。 并且您正在迭代results.response
,但看起来您想迭代results.response.posts
,这是一系列博客文章。
此外,每个帖子似乎都有一个photos
数组,您必须从中选择具有所需照片大小的元素。
例:
$.each(results.response.posts, function(i, item){
var src = item.photos[0].alt_sizes[0].url; // first picture, first size
var caption = item.caption;
$("<img/>").attr("src", src).appendTo("#submissions").wrap('<div class="postImage"></div>').after('<span class="postCaption">' + caption + '</div>');
});
当然,如果帖子没有照片并且数组为空,您将收到运行时错误,但我认为您会考虑这些情况。
另一个问题是你不能向tumblr API发出Ajax请求,因为它是一个外部域。但是,tumblr API 支持 JSONP,所以如果你在 URL 中添加&callback=?
,你告诉 jQuery 改用 JSONP:
var url = "[...]/posts?api_key=APIKEY&limit=5&callback=?";
我建议阅读一些基本的JavaScript教程,学习如何使用数组和对象:
- 雄辩的JavaScript - 数据结构:对象和数组
- MDN - 使用对象
- MDN - 预定义核心对象:数组对象
相关文章:
- 为什么不't Javascript对我的输入值进行了一些重新检查
- 可以't让我的if语句处理js中的html表单输入
- 我的单元测试选项是什么
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 我的职位回报太快了,如何做出承诺
- 我的jQuery插件参数没有正确启动,遇到了问题
- 如何识别我的网站中的慢速设备
- 如何将JSON数据导入我的ejs模板
- JQuery添加元素需要在我的js之前再次添加JQuery脚本
- 知道为什么我的旋转木马不会自动更改图片吗
- 我的模板未被解析
- 无法将数据从firebase获取到我的html页面
- 角度图表;t显示在我的页面中
- 我的AngularJS表达式没有'不起作用
- 将电视直播频道从网站嵌入我的网站
- /undefined在我的404错误日志中多次出现
- 为什么我的d3.jsselectAll+过滤器没有过滤
- Tumblr将新代码注入我的链接中
- 使用 tumblr API(JSON) 和 $.getJSON 从我的 tumblr 博客中获取最新文章
- 我如何将JavaScript添加到我的Tumblr博客上的每个非索引页面