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

2019-04-06 10:00发布

问题:

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)

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?

回答1:

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.



回答2:

  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>.



回答3:

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...



回答4:

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:

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.

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



回答5:

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.