FB init function gives wrong version error

后端 未结 20 1155
故里飘歌
故里飘歌 2020-12-08 18:18

I\'m using the Facebook JS sdk, and I have created a new App today. Everything is configured properly. Using init function like:

window.fbAsyncInit = functio         


        
相关标签:
20条回答
  • 2020-12-08 18:49

    Make sure you're using https:// in the URL

    js.src = "https://connect.facebook.net/en_US/sdk.js";
    

    I would not have believed this either, but the only difference between what Facebook has in their current sample code and what I have is https:// instead of // for the url.

    Updating to be https:// seems to have fixed it for me and I cannot duplicate the error.

    Also if you're doing any checks elsewhere in your code to see if FB is defined make sure to check if (window.FB) and not if (FB) or you will get an error.

    // call after manually adding DOM nodes containing FB widgets
    if (window.FB)
    {
        window.FB.XFBML.parse()
    }
    

    If you're using typescript you need to add something like this:

    interface Window
    {
        FB: IFacebook;
    }
    
    interface IFacebook
    {
        XFBML: any;
        ui: any;
        getLoginStatus(callback: (response: { status: string }) => any);
        login(callback: (response) => any, options: any);
        logout(callback: () => any);
        Event: any;
        api(url: string, callback: (response: any) => any);
    }
    
    0 讨论(0)
  • 2020-12-08 18:51

    This problem plagued me for a while. I tried many of the ideas listed here such as changing the version number and moving/removing the fb-root.

    What is finally working well for me is to delete the entire window.fbAsyncInit function and specify the init properties in the hash of sdk.js. For example:

    <script type="text/javascript">
        (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 = "https://connect.facebook.net/en_US/sdk.js#version=v2.2&appId=12345&status=true&cookie=true&xfbml=true";
            fjs.parentNode.insertBefore(js, fjs);
        }(document, 'script', 'facebook-jssdk'));
    </script>
    

    Of course you may omit a parameter to use its default value.

    I can't find this approach documented anywhere so use it at your own risk. Luckily I was upgrading an older site from v1.x to v2.x and it used this technique when loading all.js.

    0 讨论(0)
  • 2020-12-08 18:53

    Add &version=v2.0 to js.src, as follows:

    (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_US/sdk.js#xfbml=1&version=v2.0";
      fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
    
    0 讨论(0)
  • 2020-12-08 18:53

    The error happened each time I redirected to a page (Edge Browser) that had a fb-page plugin.

    If I refreshed the page it would work. If I got there through a hyperlink it would throw the error.

    Fixed it by adding sdk.js?d=" + new Date().toISOString(); to the script.

    (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_US/sdk.js?d=" + new Date().toISOString();
            fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
    
    0 讨论(0)
  • 2020-12-08 18:55

    Try this code:

    setInterval(
    function() { FB.api('/me/', 
        function(r) { console.log('response: ', r); })
    }, 5000);
    

    I noticed then when I'm trying get info fb still be not initialized.

    I fixed it by adding my initialization code to end of:

    FB.getLoginStatus(function(response) { init(); });
    
    0 讨论(0)
  • 2020-12-08 19:00

    i replaced this

    js.src = "//connect.facebook.net/en_US/sdk.js";
    

    with this

    js.src = "//connect.facebook.net/en_US/all.js";
    

    and worked :)

    0 讨论(0)
提交回复
热议问题