我正在使用 Facebook JavaScript API 创建 Facebook 登录功能并从 API 获取用户详细信息。
当我以正常方式使用相同的代码时,我从 Facebook api 获取数据。
当我尝试使用 requireJS 做同样的事情时,我收到此错误
“未捕获类型错误:FB.login 不是函数”
我见过像未定义函数这样的错误,但这对我来说是新的。
我遵循的格式
https://developers.facebook.com/docs/javascript/howto/requirejs/v2.7 https://developers.facebook.com/docs/javascript/howto/requirejs/v2.7
我的代码 fb.js 放在这里
define(['jquery','facebook'], function($,FB){
window.fbAsyncInit = function() {
FB.init({
appId : '89080988080545753578',
oauth : true,
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true ,// parse XFBML
version : 'v2.5'
});
};
window.fb_login = function(){
FB.login(function(response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
//console.log(response); // dump complete info
access_token = response.authResponse.accessToken; //get access token
user_id = response.authResponse.userID; //get FB UID
FB.api('/me', function(response) {
user_email = response.email; //get user email
$("#name").val(response.name);
$("#user-id").val(response.id);
if(response.gender == 'male') {
$('input[name="gender"][value="female"]').attr('checked', false);
$('input[name="gender"][value="male"]').attr('checked', true);
} else {
$('input[name="gender"][value="male"]').attr('checked', false);
$('input[name="gender"][value="female"]').attr('checked', true);
}
$("#birthday").val(response.birthday);
$("#email").val(response.email);
// you can store this data into your database
});
} else {
//user hit cancel button
console.log('User cancelled login or did not fully authorize.');
}
}, {
scope: 'publish_stream,email'
});
};
});
以及按钮点击的 html 代码
<button type="button" class="btn btn-lg btn-block sm fb" value="Login" id="FB">Facebook</button>
以及点击事件的JS代码
$( document ).ready(function() {
$(".btn").click(function(){
//getting button id value in variable
var method = $(this).attr('id');
$("#sign_method").val(method);
if(method == 'FB') {
alert('Facebook');
fb_login();
//fbData();
//$("#signup").submit();
} else if(method == 'GP') {
alert('Google Plus');
login();
// $("#signup").submit();
} else {
alert('Email');
$("#signup").remove();
alert('form removed');
window.location.href = "signup.html";
}
});