【问题标题】:Fetch WordPress API call :: how to get it to actually display on the page获取 WordPress API 调用 :: 如何让它实际显示在页面上
【发布时间】:2018-06-09 04:17:48
【问题描述】:

https://codepen.io/Mortiferr/pen/XYNLpK?editors=1011

我正在尝试让帖子显示在此处,但似乎无法正常工作。如果我 console.log data[0].title 它可以工作并提供我要求的数据。但是当我尝试在页面上显示它时......没有骰子。有关详细信息,请参阅 codepen。

【问题讨论】:

    标签: javascript wordpress fetch wordpress-rest-api


    【解决方案1】:

    看起来data.title 是一个内部有rendered 的对象。

    data[0].title.rendered 将完成这项工作

    $(document).ready(function(){
      fetch('https://www.perfectimprints.com/blog/wp-json/wp/v2/posts/')
      .then(response => response.json())
      .then(data => {
        // Here's a list of repos!
        $("#postTitle").append(data[0].title.rendered);
      });
    })
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="postDate"></div>
    <div id="postTitle"></div>

    【讨论】:

    • 你是最棒的。工作完美。非常感谢。
    • 下次你可以试试console.log(data) 并在 devtools 中查看。它将向您展示它的外观。
    猜你喜欢
    • 2017-01-30
    • 1970-01-01
    • 1970-01-01
    • 2019-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-17
    相关资源
    最近更新 更多