gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:58:58 24 4
gpt4 key购买 nike

我想显示从以下 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com