我尝试使用Blogger API v3列出所有博文:
<script type="text/javascript">
function handleResponse(response) {
var post_number = Object.keys(response.items).length; //number of posts
for (i=0; i<post_number; i++) {
$('#content').append('<div id="post' + (i+1) + '" class="post"><p></p></div>');
$('.post p').html(Object.keys(response.items[i].title));
}
}
</script>
<script src="https://www.googleapis.com/blogger/v3/blogs/1961645108677548855/posts?callback=handleResponse&key=AIzaSyAJESQB3ddltUcDbZif3LUnX-Gzr18tBRg"></script>
这会将3个div(因为3个帖子)附加到我的内容div。但每个div的内容是:
<p>
"1"
"2"
"3"
"4"
"5"
</p>
我不知道为什么,但我认为title
是items[]
的属性。
任何解决方案或线索?
感谢您的回答!
答案 0 :(得分:2)
您应该删除Object.keys()
并尝试此操作:
<script type="text/javascript">
function handleResponse(response) {
var post_number = Object.keys(response.items).length; //number of posts
for (i=0; i<post_number; i++) {
$('#content').append('<div id="post' + (i+1) + '" class="post"><p></p></div>');
$('.post p').html(response.items[i].title);
}
}
</script>
<script src="https://www.googleapis.com/blogger/v3/blogs/1961645108677548855/posts?callback=handleResponse&key=AIzaSyAJESQB3ddltUcDbZif3LUnX-Gzr18tBRg"></script>
在您的情况下,您不应使用 Object.keys()
答案 1 :(得分:0)
您请求不使用maxResults
参数并且检索了有限数量的帖子,因此我建议使用Google JavaScript Client Library - Blogger API并递归检索博客的所有帖子。
请参阅以下示例:
<script>
function renderResults(response) {
if (response.items) {
for (var i = 0; i < response.items.length; i++) {
//do whatever you want with the posts of your blog
}
}
if(response.nextPageToken) {
var blogId = 'XXX Your blogId XXX';
var request = gapi.client.blogger.posts.list({
'blogId': blogId,
'pageToken': response.nextPageToken,
'maxResults': 100,
});
request.execute(renderResults);
}
}
function init() {
gapi.client.setApiKey('XXX Get your API Key from https://code.google.com/apis/console XXX');
gapi.client.load('blogger', 'v3', function() {
var blogId = 'XXX Your blogId XXX';
var request = gapi.client.blogger.posts.list({
'blogId': blogId,
'maxResults': 100,
});
request.execute(renderResults);
});
}
</script>
<script src="https://apis.google.com/js/client.js?onload=init"></script>