1

我在以下代码中收到 javascript 错误。我得到的错误是“Uncaught ReferenceError: FB is not defined. postToFeed (anonymous function)”。有人可以帮我弄这个吗?提前致谢。

window.fbAsyncInit = function() {
        FB.init({
            appId      : '<?= APP_ID ?>', // App ID
            channelUrl : '.....', // 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));

    function postToFeed() {
        var obj = {
            method: 'feed',
            link: '<?= REDIRECT_URL ?>',
            picture: '<?= SHARE_IMAGE ?>',
            name: unescape('<?= SHARE_TITLE ?>'),
            caption: unescape('<?= SHARE_CAPTION ?>'),
            description: unescape('<?= SHARE_DESCRIPTION ?>')
        };

        function callback(response) {
            console.log(response);
            //alert("Message posted to your wall, thanks!");
            if (response && response.post_id) {
                alert('Success');
                window.location="page.php";
            }       else {
                alert('Fail.');
            }
        }
        FB.ui(obj, callback);
    }

    function sendToFriend() {
        var obj = {
            method: 'send',
            link: '<?= REDIRECT_URL ?>',
            picture: '<?= SHARE_IMAGE ?>',
            name: unescape('<?= SHARE_TITLE ?>'),
            caption: unescape('<?= SHARE_CAPTION ?>'),
            description: unescape('<?= SHARE_DESCRIPTION ?>')
        };

        function callback(response) {
            console.log(response);

            //alert("Message sent to your friend(s), thanks!");
            if (response && response.post_id) {
                alert("Success!");
                window.location="page.php";
            } else {
                alert('Fail.');
            }
        }

        FB.ui(obj, callback);
    }
4

1 回答 1

0

对于您的第二个问题(来自评论),
发送方法不会在响应中返回 post_id,这就是即使在成功发布后它也会进入 else 块的原因。检查控制台中的响应,唯一返回的是成功参数success : true

对于您的第一个问题
您调用postToFeed()太快,即 Facebook SDK 仍在加载。尝试调用相同的window.onload

于 2013-06-25T07:01:38.153 回答