FB与回调共享

时间:2014-07-15 23:01:11

标签: javascript facebook

我试图用回调来实现facebook共享功能。

我找到了这个示例脚本

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" ></script>
<script>
function fb_share() {
    FB.ui( {
        method: 'feed',
        name: "Facebook API: Tracking Shares using the JavaScript SDK",
        link: "https://www.webniraj.com/2013/05/11/facebook-api-tracking-shares-using-the-javascript-sdk/",
        picture: "https://stackexchange.com/users/flair/557969.png",
        caption: "Tracking Facebook Shares on your website or application is a useful way of seeing how popular your articles are with your readers. In order to tracking Shares, you must used the Facebook JavaScript SDK."
    }, function( response ) {
        if ( response !== null && typeof response.post_id !== 'undefined' ) {
            console.log( response );
            // ajax call to save response
          $.post( 'http://www.example.com/', { 'meta': response }, function( result ) {
                console.log( result );
            }, 'json' );
        }
    } );


}

$(document).ready(function(){
  $('.share-btn').on( 'click', fb_share );
});

</script>
</head>

<body>
    <button class='share-btn'>click</button>
</body>
</html>

然而它似乎没有用,虽然有些人说这对他们有用。当我点击按钮时没有任何反应。某处有错误吗?或许有人可以指导我另一个样本。会非常感激!!欢呼声。

1 个答案:

答案 0 :(得分:0)

好像你错过了一些东西。我快速浏览了一下Facebook JS SDK文档here,看起来您错过了SDK的源代码和使用您的应用程序凭据的FB init方法。例如:

function fb_share() {
  FB.init({
    appId      : '{your-app-id}',
    xfbml      : true,
    version    : 'v2.0'
  });
  FB.ui( {
    method: 'feed',
    name: "Facebook API: Tracking Shares using the JavaScript SDK",
    link: "https://www.webniraj.com/2013/05/11/facebook-api-tracking-shares-using-the-javascript-sdk/",
    picture: "https://stackexchange.com/users/flair/557969.png",
    caption: "Tracking Facebook Shares on your website or application is a useful way of seeing how popular your articles are with your readers. In order to tracking Shares, you must used the Facebook JavaScript SDK."
}, function( response ) {
    if ( response !== null && typeof response.post_id !== 'undefined' ) {
        console.log( response );
        // ajax call to save response
      $.post( 'http://www.example.com/', { 'meta': response }, function( result ) {
            console.log( result );
        }, 'json' );
    }
});

}

这是一个包含http://jsfiddle.net/bn7F7/所有内容的JSFiddle。

希望有所帮助!