将json Feed数据显示为html

时间:2016-05-11 07:51:56

标签: javascript html json facebook facebook-graph-api

我想显示以下网址到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>

但我没有得到任何结果。

如果我遗失任何东西,有人可以帮忙吗?

谢谢

2 个答案:

答案 0 :(得分:2)

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);
}

答案 1 :(得分:1)

试试这个

&#13;
&#13;
$.getJSON("https://graph.facebook.com/1041049395967347/posts?access_token=616050815226195|bqcTMDgKwdzdDyOeD8uyIKEYZlo",null,function(result){

    
  var out = "<table>";

    for(i = 0; i < result.data.length; i++) {
        out += "<tr><td>" + 
        result.data[i].story +
        "</td><td>" +
        result.data[i].story +
        "</td><td>" +
        result.data[i].created_time +
        "</td></tr>";
    }
    out += "</table>";
  
  $("#divContent").html(out);
  
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="divContent"></div>
&#13;
&#13;
&#13;