Facebook Javascript SDK 错误“Object [object Object] 没有方法‘init’

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22598861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:26:47  来源:igfitidea点击:

Facebook Javascript SDK error "Object [object Object] has no method 'init`

javascriptfacebookgoogle-apps-scriptfacebook-javascript-sdk

提问by Alan Wells

One of the first things that needs to be done in order to load the SDK for interacting with Facebook, is to run:

为了加载与 Facebook 交互的 SDK,需要做的第一件事是运行:

FB.init(params)

FB.init(params)

This method is used to initialize and setup the SDK. . . . . . All other SDK methods must be called after this one, because they won't exist until you do.

此方法用于初始化和设置 SDK。. . . . . 所有其他 SDK 方法必须在此之后调用,因为在您调用之前它们将不存在。

Facebook JavaScript SDK: .init()

Facebook JavaScript SDK:.init()

I'm getting the error msg:

我收到错误消息:

Object [object Object] has no method "init"

Object [object Object] has no method "init"

I'm not getting an error that FBis undefined, so I'm assuming that at least part of the Facebook Javascript SDKloaded.

我没有收到FB未定义的错误,所以我假设至少有一部分已Facebook Javascript SDK加载。

When the SDKloads, Facebook puts contents into a <div>element: <div id="fb-root"></div>

SDK加载时,Facebook 将内容放入一个<div>元素中: <div id="fb-root"></div>

Just for the fun of it, I used .innerHTMLto see if I could extract anything out of that <div>.

只是为了好玩,我过去常常.innerHTML看看是否可以从中提取任何内容<div>

var fbRootContent = document.getElementById('fb-root').innerHTML;
console.log("fbRoot Content: " + fbRootContent);

And it does show that something got injected into that <div>. So, . . . I'm assuming that at least some of the Facebook Javascript SDKloaded.

它确实表明某些东西被注入其中<div>。所以, 。. . 我假设至少有一些Facebook Javascript SDK加载。

One possible issue, is that I'm using Google Apps Script, and lots of content gets sanitized and stripped out before it is served. So it's a possibility that, even though some of the SDK is loading, some of it is getting stripped out by the sanitation process.

一个可能的问题是,我使用的是 Google Apps 脚本,并且很多内容在提供之前都会被清理和删除。因此,即使某些 SDK 正在加载,也有可能会被清理过程删除。

I'm simply trying to determine if it's possible to load the Facebook Javascript SDKinto Apps Script. I haven't had any luck so far. Here is the latest code I've used in the attempt to test whether the SDK can be loaded or not.

我只是想确定是否可以将其加载Facebook Javascript SDK到 Apps 脚本中。到目前为止,我还没有任何运气。这是我用来测试是否可以加载 SDK 的最新代码。

<div id="fb-root"></div>
<label id="lblToHid">Load Facebook Javascript SDK</label>

<br>
<br>
<button id="btnTest">test</button>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="https://connect.facebook.net/en_US/all.js"></script>

<script>
$(document).ready(function() {
$("#lblToHid").hide();
  $("#btnTest").click(function(){
  $("#lblToHid").show();
});

callFB();
fbAsyncInit();

});

function callFB(){
  console.log("callFB ran: ");
    window.fbAsyncInit = function() {
      console.log("AsyncInit ran");

  console.log("Here is the object FB: " + FB);

  var fbRootContent = document.getElementById('fb-root').innerHTML;
  console.log("fbRoot Content: " + fbRootContent);

    FB.init({
        appId      : 'myAppID Here',
        status     : true, // check login status
        cookie     : true, // enable cookies to allow the server to access the session
        oauth      : true, // enable OAuth 2.0
        xfbml      : true  // parse XFBML
      });

  console.log("callFB done running: ");

  };
};
</script>

Note that I am not loading the SDK asynchronously. I'm doing that on purpose to test if it will load or not. Note the line:

请注意,我不是异步加载 SDK。我这样做是为了测试它是否会加载。请注意这一行:

<script src="https://connect.facebook.net/en_US/all.js"></script>

Facebook suggests loading the SDK a different way. Why? Because it might slow down the initial load of your app.

Facebook 建议以不同的方式加载 SDK。为什么?因为它可能会减慢应用程序的初始加载速度。

So, why am I getting the error: Object [object Object] has no method "init"

那么,为什么我会收到错误消息: Object [object Object] has no method "init"

And is it possible to load the Facebook Javascript SDKinto Google Apps Script?

是否可以将其加载Facebook Javascript SDK到 Google Apps 脚本中?

I'm using jQuery: $(document).ready,

我正在使用 jQuery: $(document).ready,

jQuery $(document).ready()

jQuery $(文档).ready()

so FB.initshouldn't be getting called until the <script>tag has loaded the Facebook SDK.

所以FB.init<script>标签加载 Facebook SDK之前不应该被调用。

回答by Brad The Developer

Try changing your Facebook script import to this.

尝试将您的 Facebook 脚本导入更改为此。

<script src="//connect.facebook.net/en_US/all.js#xfbml=1&appId={your-app-id}"></script>

Where {your-app-id}is your actual app id.

其中{your-app-id}是您的实际应用 ID。

Update

更新

Have you tried this.

你有没有试过这个。

function callFB(){
console.log("callFB ran: ");
window.fbAsyncInit = function() {
  console.log("AsyncInit ran");

FB.init({
    appId      : 'myAppID Here',
    status     : true, // check login status
    cookie     : true, // enable cookies to allow the server to access the session
    oauth      : true, // enable OAuth 2.0
    xfbml      : true  // parse XFBML
  });

console.log("Here is the object FB: " + FB);
 var fbRootContent = document.getElementById('fb-root').innerHTML;
console.log("fbRoot Content: " + fbRootContent);
console.log("callFB done running: ");

};
};