未捕获TypeError:FB.login不是一个函数 [英] Uncaught TypeError: FB.login is not a function
问题描述
我正在使用Facebook JavaScript API创建具有Facebook功能的登录功能,并从API获取用户详细信息。
I am Using Facebook JavaScript API to create login with feature with Facebook and fetch the user details from the API.
当我以正常方式使用相同的代码我正在从Facebook api获取数据。
When I am using the same code in normal way, I am getting the data fetched from from Facebook api.
当我尝试用requireJS做同样的事情时,我得到这个错误
When I try to do the same thing with requireJS, I am Getting this error
未捕获TypeError:FB.login不是函数
"Uncaught TypeError: FB.login is not a function"
我看到错误,如未定义的功能,但对我来说这是新的。
I have seen errors like undefined function but this new to me.
我遵循的格式
a href =https://developers.facebook.com/docs/javascript/howto/requirejs/v2.7 =nofollow> 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";
}
});
推荐答案
错误信息表示JS SDK尚未加载。你在代码中缺少这个:
The error message means that the JS SDK is not loaded yet. You are missing this in your code:
(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 = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
必须使用FB.login
code> FB.init 。确保 FB.init
被调用。
FB.login
must be used AFTER FB.init
. Make sure FB.init
gets called.
更多信息: http://www.devils-heaven.com/facebook-javascript-sdk-login/
对于RequireJS,这将是包含JS SDK的官方方式: https://developers.facebook.com/docs/javascript/howto/requirejs/v2.7#adding-a-shim- to-the-facebook-sdk
For RequireJS, this would be the official way to include the JS SDK: https://developers.facebook.com/docs/javascript/howto/requirejs/v2.7#adding-a-shim-to-the-facebook-sdk
这篇关于未捕获TypeError:FB.login不是一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!