Facebook的JavaScript SDK

时间:2014-01-13 16:31:46

标签: javascript facebook

我正在尝试开始使用Facebook的JavaScript SDK,但在尝试在我的wamp服务器中运行html文件时,我总是遇到以下错误:

Uncaught SecurityError: Blocked a frame with origin "http://static.ak.facebook.com" from accessing a frame with origin "http://localhost:9000". Protocols, domains, and ports must match. VM631:1
Uncaught SecurityError: Blocked a frame with origin "https://s-static.ak.facebook.com" from accessing a frame with origin "http://localhost:9000".  The frame requesting access has a protocol of "https", the frame being accessed has a protocol of "http". Protocols must match.
 VM638:1
Uncaught SecurityError: Blocked a frame with origin "http://static.ak.facebook.com" from accessing a frame with origin "http://localhost:9000". Protocols, domains, and ports must match. 

这是我的测试代码:          

<div id="fb-root"></div>
<script>
    window.fbAsyncInit = function() {
        // init the FB JS SDK
        FB.init({
            appId      : 'APP_ID',                     // App ID from the app dashboard
            status     : true,                                 // Check Facebook Login status
            xfbml      : true                                  // Look for social plugins on the page
        });

        // Additional initialization code such as adding Event Listeners goes here
    };

    // Load the SDK asynchronously
    (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/all.js";
        fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));

</script>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

您是否尝试过基于jQuery的Facebook SDK? 以下片段可能对您有所帮助

$(document).ready(function() {
      $.ajaxSetup({ cache: true });
      $.getScript('//connect.facebook.net/en_UK/all.js', function(){
        FB.init({
          appId: 'YOUR_APP_ID',
        });     
        $('#loginbutton,#feedbutton').removeAttr('disabled');
        FB.getLoginStatus(updateStatusCallback);
      });
    })
  

注意:此示例使用jQuery,因此您需要包含它

答案 1 :(得分:0)

试试这个......:)

    <div id="fb-root"></div>
    <script>
        window.fbAsyncInit = function() {               
            FB.init({
                appId      : '**appId**',
                channelUrl : '//local.facebook-test/channel.html',
                status     : true,
                xfbml      : true,
                oauth      : true
            });
            FB.login(function(response)
            {
                if (response.authResponse)
                {
                    console.log(response.authResponse.accessToken);
                     var opts = {
                        message : '**message**',
                        name : '**name**',
                        link : 'https://developers.facebook.com/docs/reference/dialogs/'                                                        
                     };
                    FB.api('/me/feed', 'post', opts, function(response)
                     {
                        if (!response || response.error)
                        {
                            console.log(response.error);
                            alert('Posting error occured');
                        }else{
                            alert('Success - Post ID: ' + response.id);
                        }
                     });
                } else {
                    alert('Not logged in');
                }
            }, { scope: 'manage_pages, publish_actions, user_photos' });

        };
        (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/all.js";
            fjs.parentNode.insertBefore(js, fjs);
        }(document, 'script', 'facebook-jssdk'));
    </script>
</body>