Javascript 带回调的FB共享
我正在尝试通过回调实现facebook共享功能 我找到了这个示例脚本Javascript 带回调的FB共享,javascript,facebook,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: "Fac
<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>
函数fb_share(){
FB.ui({
方法:“提要”,
名称:“Facebook API:使用JavaScript SDK跟踪共享”,
链接:“https://www.webniraj.com/2013/05/11/facebook-api-tracking-shares-using-the-javascript-sdk/",
图片:“https://stackexchange.com/users/flair/557969.png",
描述:“在您的网站或应用程序上跟踪Facebook共享是查看您的文章在读者中受欢迎程度的有用方法。为了跟踪共享,您必须使用Facebook JavaScript SDK。”
},功能(回应){
if(response!==null&&typeof response.post\u id!=='undefined'){
控制台日志(响应);
//保存响应的ajax调用
$.post($)http://www.example.com/“,{'meta':响应},函数(结果){
控制台日志(结果);
}“json”);
}
} );
}
$(文档).ready(函数(){
美元('share btn')。在('click',fb_share');
});
点击
然而,它似乎不起作用,尽管有些人说它对他们有效。当我点击按钮时,什么也没发生。哪里有错误吗?或者有人可以告诉我另一个样本。非常感谢!!干杯。看来你错过了一些东西。我快速查看了Facebook JS SDK文档,似乎您缺少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' );
}
});
}
这里是一个包含所有内容的JSFIDLE
希望有帮助 嘿,非常感谢!它现在确实起作用了,但有一个小问题。只有我可以使用共享功能。。。另一个错误是:发生了错误。我检查了facebook设置中的URL,一切似乎都很好。为什么只有我可以使用共享功能?对不起,我真蠢。。。该应用程序没有公开。再次非常感谢!令人惊叹的!如果我满意地回答了您的问题,您可能会检查以下内容::)@Boomish我应该在link和$中输入哪个URL。post我使用了我的localhost URL并显示了错误。