如何解决未定义的错误函数

时间:2015-09-11 10:32:36

标签: jquery facebook-javascript-sdk

我正在尝试在Facebook墙上发布图像我使用以下代码:

<script type="text/javascript">
window.fbAsyncInit = function() {
    FB.init({
    appId      : 'XXXXXXXX', // replace your app id here
    channelUrl : 'http://localhost/channel.html', 
    status     : true, 
    cookie     : true, 
    xfbml      : true  
    });
};
(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 FBLogin(){
    FB.login(function(response){
        if(response.authResponse){
            window.location.reload();
        }
    }, {scope: 'email'});
}
function post_on_wall()
{
            var share_name          = 'rohitashav';
            var share_link          = 'http://rohitashavsinghal.com';
            var share_image         = 'http://rohitashavsinghal.com/fb/love.jpg';
            var share_caption       = 'testing';
            var share_description   = 'test desc';

            var share_redirect      = 'http://rohitashavsinghal.com/';


            FB.ui(
            {
                method:         'feed',
                name:           share_name,
                link:           share_link,
                picture:        share_image,
                caption:        share_caption,
                description:    share_description
                //message:      ''
            },
            function(response) 
            {
                if (response && response.post_id) 
                {
                    alert('thanks for sharing!');
                    window.top.location.href = share_redirect;
                } 
                else 
                {
                    alert('Post was not published.');
                }
            }
}
</script>

并按如下方式创建按钮:

<input type="button" value="Post on Wall" onClick="post_on_wall();" />

当我点击此按钮然后在console中我收到以下错误:

我已经定义了这个函数

post_on_wall is not defined

为什么我收到此错误?

2 个答案:

答案 0 :(得分:1)

您的FB.ui来电尚未结束。您需要在回调函数后添加);。没有它,post_on_wall函数无效。说实话,我真的很惊讶你没有在控制台中收到错误。

此外,您上面包含Facebook JS的电话错误)。您需要将}(document));更改为})(document);

答案 1 :(得分:1)

我发现代码中存在一些错误。

1

 (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));`

如果我的Javascript正确,那应该是这样的:

(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);
  1. FB.UI来电中,您有几处错误:

    FB.ui( //method opened
        {
            method:         'feed',
            name:           share_name,
            link:           share_link,
            picture:        share_image,
            caption:        share_caption,
            description:    share_description
            //message:      ''
        },
        function(response) 
        {
            if (response && response.post_id) 
            {
                alert('thanks for sharing!');
                window.top.location.href = share_redirect;
            } 
            else 
            {
                alert('Post was not published.');
            }
    
  2. 现在,打开的方法调用没有匹配的右括号)