繁体   English   中英

Facebook的JavaScript SDK

[英]Facebook's JavaScript SDK

我正在尝试开始使用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>

你有没有尝试过基于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,因此您需要包含它

试试吧.. :)

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

暂无
暂无

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

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