对于 新开发者 尝试在其网站中集成 facebook 登录和身份验证的用户,如果他们只是尝试从 Facebook 开发人员帮助中复制教程,则可能会收到错误消息。
错误是:未捕获的 ReferenceError:statusChangeCallback 未定义
代码:
<script>
function checkLoginState() {
FB.getLoginStatus(function (response) {
statusChangeCallback(response);
});
}
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "https://connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
window.fbAsyncInit = function () {
FB.init({
appId: '<Your App ID>',
cookie: true,
xfbml: true,
version: 'v2.12'
});
FB.AppEvents.logPageView();
};
</script>
<fb:login-button scope="public_profile,email"
onlogin="checkLoginState();">
</fb:login-button>
<div id="status"></div>
最佳答案
解决办法是新开发者是忘记写成功回调函数 在他们的代码中。当他们尝试从 Facebook 应用程序注册后立即出现的 Facebook 指南中复制代码时,就会出现问题。
在你的 HTML 中定义一个 id 状态为 <div id="status"></div>
的 div就在 <script>
之前标记。
请包括下面的 javascript 方法以使 API 工作。我写了一些 console.log 详细的文本。您可以删除此文本并替换为您的文本。
function statusChangeCallback(response) {
console.log('statusChangeCallback');
console.log(response);
// The response object is returned with a status field that lets the
// app know the current login status of the person.
// Full docs on the response object can be found in the documentation
// for FB.getLoginStatus().
if (response.status === 'connected') {
// Logged into your app and Facebook.
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function (response) {
console.log('Successful login for: ' + response.name);
document.getElementById('status').innerHTML =
'Thanks for logging in, ' + response.name + '!';
});
} else {
// The person is not logged into your app or we are unable to tell.
document.getElementById('status').innerHTML = 'Please log ' +
'into this app.';
}
}
关于facebook-graph-api - “statusChangeCallback”未定义 Facebook Javascript SDK 登录错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49468165/