简体   繁体   English

Facebook Javascript SDK错误“对象[object Object]没有方法'init'

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

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)

This method is used to initialize and setup the SDK. 此方法用于初始化和设置SDK。 . . . . . All other SDK methods must be called after this one, because they won't exist until you do. 在此之后,必须调用所有其他SDK方法,因为它们只有在您执行之后才存在。

Facebook JavaScript SDK: .init() Facebook JavaScript SDK:.init()

I'm getting the error msg: 我收到错误消息:

Object [object Object] has no method "init"

I'm not getting an error that FB is undefined, so I'm assuming that at least part of the Facebook Javascript SDK loaded. 我没有得到未定义FB的错误,因此我假设至少加载了Facebook Javascript SDK一部分。

When the SDK loads, 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 .innerHTML to 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> . 它的确表明向<div>注入了一些东西。 So, . 因此,。 . . I'm assuming that at least some of the Facebook Javascript SDK loaded. 我假设至少加载了一些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. 一个可能的问题是,我正在使用Google Apps脚本,因此很多内容在投放之前都经过了清理和剥离。 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. 因此,即使某些SDK正在加载,但仍有一部分卫生过程会将其剥离。

I'm simply trying to determine if it's possible to load the Facebook Javascript SDK into Apps Script. 我只是想确定是否有可能将Facebook Javascript SDK加载到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. 这是我用来测试是否可以加载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. 请注意,我不是异步加载SDK。 I'm doing that on purpose to test if it will load or not. 我这样做是为了测试它是否可以加载。 Note the line: 请注意以下行:

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

Facebook suggests loading the SDK a different way. Facebook建议以另一种方式加载SDK。 Why? 为什么? Because it might slow down the initial load of your app. 因为这可能会减慢您的应用程序的初始负载。

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 SDK into Google Apps Script? 是否可以将Facebook Javascript SDK加载到Google Apps脚本中?

I'm using jQuery: $(document).ready , 我正在使用jQuery: $(document).ready

jQuery $(document).ready() jQuery $(文档).ready()

so FB.init shouldn't be getting called until the <script> tag has loaded the Facebook SDK. 因此,在<script>标记加载Facebook SDK之前,不应调用FB.init

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: ");

};
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM