Tôi đang cố kiểm tra xem người dùng đã đăng nhập bằng facebook chưa và gặp lỗi đó trong bảng điều khiển JS. Mã của tôi trông giống như sau:Tham chiếu không bắt buộcLỗi: FB không được xác định khi sử dụng FB.getLoginStatus
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId: '######', // App ID
status: true, // check login status
cookie: true, // enable cookies to allow the server to access the session
xfbml: true // parse XFBML
});
};
// Load the SDK Asynchronously
(function (d) {
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) { return; }
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
FB.getLoginStatus(function (response) {
if (response.status === 'connected') {
// the user is logged in and has authenticated your
// app, and response.authResponse supplies
// the user's ID, a valid access token, a signed
// request, and the time the access token
// and signed request each expire
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
} else {
// the user isn't logged in to Facebook.
}
});
</script>
Điều gì có thể là vấn đề và có bất kỳ sugestions nào về cách khắc phục?
Bạn đang kêu gọi 'FB.getLoginStatus' trước SDK được nạp và/hoặc được khởi tạo.Để chờ điều đó, đó là những gì sự kiện 'fbAsyncInit' dành cho. Vì vậy, hãy gọi phương thức trong đó. – CBroe
Điều đó đã khắc phục được sự cố, cảm ơn! – Xeen