javascript - 将 json 提要数据显示为 html

标签 javascript html json facebook facebook-graph-api

我想显示从以下 url 到 html 页面的数据:

https://graph.facebook.com/1041049395967347/posts?access_token=616050815226195|bqcTMDgKwdzdDyOeD8uyIKEYZlo

(显示来自 facebook 的最新帖子)。

这里是目前为止的代码:

<!DOCTYPE html>
<html>

<body>

<h1>Customers</h1>
<div id="id01"></div>

<script>
var xmlhttp = new XMLHttpRequest();
var url = "https://graph.facebook.com/1041049395967347/posts?access_token=616050815226195|bqcTMDgKwdzdDyOeD8uyIKEYZlo";


xmlhttp.open("GET", url, true);
xmlhttp.send();

function myFunction(response) {
    var arr = JSON.parse(response);
    var i;
    var out = "<table>";

    for(i = 0; i < arr.length; i++) {
        out += "<tr><td>" + 
        arr[i].story +
        "</td><td>" +
        arr[i].story +
        "</td><td>" +
        arr[i].created_time +
        "</td></tr>";
    }
    out += "</table>";
    document.getElementById("id01").innerHTML = out;
}
</script>

</body>
</html>

但我没有得到任何结果。

如果我遗漏了什么,有人可以帮忙吗?

谢谢

最佳答案

arr 不是一个数组而是一个对象,包含元素 datapaging,因此迭代元素不起作用。相反,你应该使用 arr.length:

var xmlhttp = new XMLHttpRequest();
var url = "https://graph.facebook.com/1041049395967347/postsaccess_token=616050815226195|bqcTMDgKwdzdDyOeD8uyIKEYZlo";

xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        myFunction(xmlhttp.responseText);
    }
}
xmlhttp.open("GET", url, true);
xmlhttp.send();

function myFunction(response) {
  var arr = JSON.parse(response);
  var i;
  var out = "<table>";

  for (i = 0; i < arr.data.length; i++) {
        out += "<tr><td>" + 
        arr.data[i].story +
        "</td><td>" +
        arr.data[i].story +
        "</td><td>" +
        arr.data[i].created_time +
        "</td></tr>";
  }
  out += "</table>";
  $("#divContent").html(out);
}

关于javascript - 将 json 提要数据显示为 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37156313/

相关文章:

javascript - 两个最大尺寸并排的标签文本区域

javascript - 多层视差,数学题

javascript - 按钮不会从其行中删除用户。 AngularJS

javascript - 浏览器是否支持不同的 HTML5 模式正则表达式功能?

java - 使用 json lib 从文本文件读取多个 json 对象

json - 如何在 golang 自定义结构类型中绑定(bind) json stringify 数据?

php - 将返回的 JSON 数据解码为对象时出现问题

javascript - AJAX、JS和Fancybox,如何使其正常工作?

html - 如何访问 *ngFor 中的 'index' 并对其进行编辑(无法删除列表中的多个项目)

php - HTML 公式重定向到 XAMPP 本地主机主页