2014-07-15 42 views
0

即時通訊嘗試實現帶回調的Facebook分享功能。帶回調的FB分享

我發現這個示例腳本

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

但它似乎沒有工作,雖然有些人說,這爲他們工作。當我點擊按鈕什麼都沒有發生。某處有錯誤嗎?或者,也許有人可以指示我去另一個樣本。將非常感謝!乾杯。

回答

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。

希望有幫助!

+0

嘿,非常感謝!它現在確實有效,但有一個小小的問題。只有我可以使用共享功能...其他出錯說:出現錯誤。我檢查了Facebook設置中的網址,一切似乎都沒有問題。爲什麼只有我可以使用共享功能? – zet

+0

對不起,我愚蠢...該應用程序沒有公開。再次感謝! – zet

+1

太棒了!如果我滿意地回答了您的問題,那麼您可以檢查一下:http://stackoverflow.com/help/someone-answers :) – Boomish