javascript - Facebook Graph API:在 HTML 网页上显示来自控制台的 JSON 数据
问题描述
完全是 Facebook 的 API 和 JavaScript 的新手,所以请多多包涵。
我目前正在尝试:
- 使用 Facebook Graph API 检索有关 Facebook 页面事件的数据。使用 JavaScript SDK。
- 在其他静态 HTML 网站上插入接下来三个事件的名称、日期和 URL(见下图)
现在的状态是我成功获取了 Facebook 页面事件的 JSON 数据,并且当我运行console.log(response)
.
但是,作为一个 JS 新手,我怎样才能得到这些数据(最好只用于接下来的三个事件)确实如下图所示显示在我的 HTML 中?
期待看到您的回复!
HTML
window.fbAsyncInit = function() {
FB.init({
appId : 'BLAH',
autoLogAppEvents : true,
xfbml : true,
version : 'v10.0'
});
FB.api(
'/BLAH/events?fields=start_time,id,name',
'GET',
{access_token:'BLAH BLAH',
},
function(response) {
console.log(response)
}
);
};
<a href="#" class="d-inline-flex text-decoration-none align-items-center my-1 hvr-bubble-float-right">
<div class="arrangement-dato text-white text-center lh-1 d-flex align-items-center flex-column justify-content-center">
<div class="dato">13</div>
<div>feb</div>
</div>
<h2 class="ms-2 link-dark">Event name</h2>
</a>
<a href="#" class="d-inline-flex text-decoration-none align-items-center my-1 hvr-bubble-float-right">
<div class="arrangement-dato text-white text-center lh-1 d-flex align-items-center flex-column justify-content-center">
<div class="dato">4</div>
<div>mar</div>
</div>
<h2 class="ms-2 link-dark">Event name</h2>
</a>
<a href="#" class="d-inline-flex text-decoration-none align-items-center my-1 hvr-bubble-float-right">
<div class="arrangement-dato text-white text-center lh-1 d-flex align-items-center flex-column justify-content-center">
<div class="dato">12</div>
<div>apr</div>
</div>
<h2 class="ms-2 link-dark">Event name</h2>
</a>
控制台中返回的 JSON 数据示例
id: "1234567890"
name: "Event name"
start_time: "2021-02-20T09:00:00+0100"
PS:我知道我不应该在客户端运行访问令牌。这是我稍后会解决的问题。
PPS:在我的 JS 代码中实际上并没有说“BLAH”。
解决方案
你用的是什么框架?如果你使用纯 JavaScript,你可以这样做:
document.getElementById(<<id of text>>).innerText = response.name;
var date = new Date(response.start_time);
document.getElementById(<<id of green circle>>).innerHTML = date.getDate() + "<br>" + date.getMonth();
如果您使用 jQuery,则将 document.getElementByID() 替换为 $('#' + <>) 并将 innerText 更改为 .text(response.name) 并将 innerHTML 更改为 .html(date.getDate() + "
" + date .getMonth())。
我希望数据是一个数组,然后你必须遍历你的响应对象(搜索循环)。
如果您使用的是 Vue、React 或 Angular,则必须在 Google 上查看如何在此处进行更改。
推荐阅读
- socks - 用于 ipv6 的 Dante SOCKS5 代理服务器配置
- xml - 如何使用元素属性将一些 xml 转换为固定长度的字符串来定义值长度
- mysql - 搜索类别计数到一个 id
- google-apps-script - 用户使用情况报告 - 未显示/未定义的 Gmail 参数(Google Admin SDK - 报告 API)
- node.js - 错误:调用者在节点中没有谷歌聊天机器人的权限
- flutter - 如何在颤动的静态类中传递上下文或任何参数?
- git - 将其他分支合并到我的分支并将太多提交推送到远程
- java - 解决“多个 Jar 文件”又名“从多个位置扫描”
- android - viewpager2 与片段,其中 viewpager2 在销毁第一个父亲片段后也没有被销毁
- jquery - 如何使用 jquery 将“2020-10-08 09:38:08”转换为 2020 年 3 月格式