2017-03-01 153 views
1

我想显示使用json和jquery的帖子列表。如何获取Json对象数组值?

这是我在用获取数据

var app = { 

    init: function() { 
     app.getPosts(); 
    }, 

    getPosts: function() { 

     var rootURL = 'https://www.example.com/wp-json/wp/v2/posts'; 

     $.ajax({ 
      type: 'GET', 
      url: rootURL, 
      dataType: 'json', 
      success: function(data){ 

       $.each(data, function(index, value) { 
        console.log(value.featured_image); 
        $('ul.topcoat-list').append('<li class="topcoat-list__item">' + 
        '<h3>'+value.title+'</h3>' + 
        '<p>'+value.excerpt+'</p></li>'); 
        ; 
       }); 
      }, 
      error: function(error){ 
       console.log(error); 
      } 

     }); 

    } 

} 

但是这个代码仅返回以下 output

但是当我去https://www.example.com/wp-json/wp/v2/posts我看到了完美的阵列。

数组的例子有

{ 
    "id":2267, 
    "type":"post", 
    "title":{"rendered":"When to visit Arugam Bay? &#8211; Arugam Bay Weather &#038; Seasons \u2013"}, 
    "content":{"rendered":"<div class=\"circle-headline\">\n<p>The right answer is<\/p>\n<\/div>\n<p class=\"wpk-circle-title text-custom\">ALL YEAR LONG!<\/p>\n<p>You can visit Arugam Bay anytime and always find amazing sunny weather. Despite this incredible where, there is a high and low season.<\/p>\n","protected":false}, 
    "excerpt":{"rendered":"<p>The right answer is<\/p>\n<p>ALL YEAR LONG!<br \/>\nYou can visit Arugam Bay anytime and always find amazing sunny weather. Despite this incredible where, there is a high and low season.<\/p>\n","protected":false}, 
    "author":1, 
    "featured_media":2268, 
    "comment_status":"open", 
    "ping_status":"open", 
    "sticky":false, 
    "template":"", 
    "format":"standard", 
} 

我怎么能拿到冠军,并摘录从那里,并用它显示一个列表?

+0

了5分钟之内两轮投票,没有任何解释。如果有人想downvote至少让我知道为什么觉得这有扁平的胸部..为什么你不喜欢它.. –

+0

这不是我downvote,但我认为downvoters认为你的问题可以通过编写简单的调试逻辑轻松解决。更多地使用'console.log()'函数来调试结果。 – Raptor

回答

4

访问包含实际字符串的渲染特性:

'<h3>'+value.title.rendered+'</h3>' + 
'<p>'+value.excerpt.rendered+'</p></li>'); 
... 
+0

似乎是正确的。 +1 –

+0

不错的努力...... + 1 –