javascript Facebook SDK 无法加载...all.js GET 失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9847357/
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-26 08:01:24  来源:igfitidea点击:

Facebook SDK doesn't load...all.js GET failing

javascriptfacebookrestget

提问by zakdances

I'm using the same code provided in the here

我正在使用此处提供的相同代码

<div id="fb-root"></div>
<script>
    window.fbAsyncInit = function() {
    console.log('got here');
    FB.init({
    appId      : '197112467099018', // App ID
    channelUrl : '//WWW.MYDOMAIN.COM/channel.html', // Channel File
    status     : true, // check login status
    cookie     : true, // enable cookies to allow the server to access the session
    xfbml      : true  // parse XFBML
   });

   // Additional initialization code here

   };

   // Load the SDK Asynchronously
   (function(d){
    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.js";
    ref.parentNode.insertBefore(js, ref);
   }(document));
 </script>

Chrome's console is telling me the script failed to load (GET failed) with 2 errors which are labeled (anonymous function)

Chrome 的控制台告诉我脚本无法加载(GET 失败),有 2 个标记为(匿名函数)的错误

The lines it points to as the errors are:

它指向错误的行是:

   ref.parentNode.insertBefore(js, ref);
   }(document));

Can anyone help me figure out whats wrong with these lines?

谁能帮我弄清楚这些行有什么问题?

回答by chmielot

I was using ghostery in chrome and didn't notice it blocked 'facebook connect'. I bet you have a similiar problem. Just happened to me.

我在 chrome 中使用 ghostery,并没有注意到它阻止了“facebook 连接”。我敢打赌你也有类似的问题。刚刚发生在我身上。

回答by Eugene

  1. Make sure you're running this code in the context of a web server (your browser URL starts with http: or https: protocol), and not from a local file (should not start with file:).

  2. Make sure you put that snippet in the <body>and not <head>.

  1. 确保您在 Web 服务器的上下文中运行此代码(您的浏览器 URL 以 http: 或 https: 协议开头),而不是从本地文件(不应以 file: 开头)。

  2. 确保你把那个片段放在了 <body>而不是<head>.

回答by Narrim

Avast Online Security a browser Extension/Addon depending on the browser version you use, has just released a new version (2014) that blocks Social scripts from running...basically they have targetted the google+, facebook, twitter CDN scripts and blocked them...and its ON by default... The funny thing is the options says it stops people tracking you but it complete ignores the google analytics script. LOL avast...nice try...

Avast Online Security 是一个浏览器扩展/插件,具体取决于您使用的浏览器版本,它刚刚发布了一个新版本 (2014),该版本会阻止社交脚本运行……基本上,他们已经针对 google+、facebook、twitter CDN 脚本并阻止了它们。 ..默认情况下它是开启的......有趣的是选项说它会阻止人们跟踪你,但它完全忽略了谷歌分析脚本。大声笑 avast...不错的尝试...

回答by Adam Pedley

Facebook has certainly changed that sample code. I orignally thought it was because js.src started with // but the JS SDK may be automatically appending the http: or https:

Facebook 肯定已经更改了该示例代码。我最初认为这是因为 js.src 以 // 开头,但 JS SDK 可能会自动附加 http: 或 https:

My code goes something like:

我的代码是这样的:

 <script type="text/javascript">
    var fbAppId = 'xxxxxxxxxx'

    window.fbAsyncInit = function () {
        FB.init({ appId: fbAppId, status: true, cookie: true,
            xfbml: true
        });
    };
    (function () {
        var e = document.createElement('script');
        e.type = 'text/javascript';
        e.src = document.location.protocol +
      '//connect.facebook.net/en_US/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
    } ());   
</script>

notice how mine has document.location.protocol before the url.

注意我的在 url 之前有 document.location.protocol 。

Try it that was and see if that helps. My code is obviously an older but still working version.

尝试一下,看看是否有帮助。我的代码显然是一个较旧但仍在工作的版本。

回答by Nimesh Jain

Check for any antivirus plugin/extension. I have Avast which has a browser plugin/extension. I changed the social networking settings and it worked for me.

检查任何防病毒插件/扩展。我有 Avast,它有一个浏览器插件/扩展。我更改了社交网络设置,它对我有用。