我不确定为什么这不起作用。我想在我的博客页面上显示帖子标题,但是我没有显示任何对象。这是我的代码:
$(document).on('pagebeforeshow', '#blogposts', function() {
//$.mobile.showPageLoadingMsg();
$.ajax({
url: "http://howtodeployit.com/category/daily-devotion/?json=recentstories&callback=",
dataType: "json",
jsonpCallback: 'successCallback',
async: true,
beforeSend: function() { $.mobile.showPageLoadingMsg(true); },
complete: function() { $.mobile.hidePageLoadingMsg(); },
success:function(data){
console.log(data.posts);
alert (data.posts.length);
},
error: function (request,error) {
alert('Network error has occurred please try again!');
}
});
});
这是我的控制台显示的内容:
[Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]
0: Object
1: Object
2: Object
3: Object
4: Object
当我展开第一个对象时,我会看到我想要的不同元素。我如何向下扩展每个对象以获得所需的输出。
答案 0 :(得分:1)
这些也是对象,尝试迭代它们:
console.log(data.posts);
for (var i = 0; i < data.posts.length; i++) {
//Just the title
console.log(data.posts[i].title);
//Iterate all the keys
for (var key in data.posts[i]) {
console.log(data.posts[i][key]);
}
}