调用FB.init()之前调用了FB.getLoginStatus()。我该如何使用Facebook API?

5

我使用这段代码来准备使用FaceBook API:

(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_GB/all.js#xfbml=1";
    fjs.parentNode.insertBefore(js, fjs);
} (document, 'script', 'facebook-jssdk'));

然而,在文档加载时我遇到了以下错误:
FB.getLoginStatus() called before calling FB.init().

我不理解这个消息。我已经在谷歌上搜索了一些论坛帖子,但是很不幸它们都没有对我的问题有所帮助。由于我对Facebook API不熟悉,因此无法解决这个问题,所以我决定在这里发布一个问题。如果我的问题对任何人来说太简单了,请原谅,并提前感谢任何答案。


1
就像字面意思一样 - 在调用任何其他 FB 函数之前,您需要调用 FB.init() 函数来初始化您的应用信息。您是否已经阅读了 SDK 文档 - freejosh
2个回答

5
我有相同的Facebook登录。
 ***FB.getLoginStatus() called before calling FB.init().***

当文档加载时,我发现在结尾处有#xfbml=1
js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1";

如果您出现了这个错误,请删除#xfbml=1,问题就会解决!


0
window.fbAsyncInit = function () {
    // init the FB JS SDK
    FB.init({
        appId: MS.FBAppID, // App ID from the App Dashboard
        //channelUrl: '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File for x-domain communication
        status: true, // check the login status upon init?
        cookie: true, // set sessions cookies to allow your server to access the session?
        xfbml: true  // parse XFBML tags on this page?
    });

    // Additional initialization code such as adding Event Listeners goes here

};

// Load the SDK's source Asynchronously
// Note that the debug version is being actively developed and might 
// contain some type checks that are overly strict. 
// Please report such bugs using the bugs tool.
(function (d, debug) {
    var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
    if (d.getElementById(id)) { return; }
    js = d.createElement('script'); js.id = id; js.async = true;
    js.src = "//connect.facebook.net/en_US/all" + (debug ? "/debug" : "") + ".js";
    ref.parentNode.insertBefore(js, ref);
} (document, /*debug*/false));

我必须使用为此应用程序创建的Facebook AppID作为AppID,并确保域名被正确定义。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接