使用fb.ui时如何检测用户取消的共享

Abh*_*jar 5 javascript facebook

我正在使用此处提供的文档和以下代码.共享对话框正确显示.问题是我无法区分用户在对话框中执行的"取消"和"发布"操作.我想这会是回应的一部分.

FB.ui({
    method: 'share',
    href: 'https://developers.facebook.com/docs/',
}, function(response){
    if (response && !response.error_code) {
        console.log(response);
    } else {
        alert('Error while posting.');
    }
});
Run Code Online (Sandbox Code Playgroud)

编辑:从控制台输出不是没有提供任何知道的方式

Cancel - Object {e2e: "{"submit_0":1401181811121}"} 
Post - Object {e2e: "{"submit_0":1401181815112}"} 
Run Code Online (Sandbox Code Playgroud)

Tob*_*obi 4

我测试了这个,显然对象中有一些信息response可以用来确定对话框是否被取消。

代码

FB.ui({
    method: 'share',
    href: 'https://developers.facebook.com/docs/'
}, function(response){
    if (response && !response.error_code) {
        console.log("OK: "+JSON.stringify(response));
    } else {
        console.log("Not OK: "+JSON.stringify(response));
    }
});
Run Code Online (Sandbox Code Playgroud)

取消时的输出:

{error_code: 4201, error_message: "User+canceled+the+Dialog+flow", e2e: "{"submit_0":1401188820613}"} 
Run Code Online (Sandbox Code Playgroud)

所以,我想你可以像这样检查取消操作:

FB.ui({
    method: 'share',
    href: 'https://developers.facebook.com/docs/'
}, function(response){
    if (response && !response.error_code) {
        console.log("OK: "+JSON.stringify(response));
    } else if (response && response.error_code === 4201) { //Cancelled
        console.log("User cancelled: "+decodeURIComponent(response.error_message));
    } else {
        console.log("Not OK: "+JSON.stringify(response));
    }
});
Run Code Online (Sandbox Code Playgroud)

不幸的是,FB.Events.subscribe()没有提供取消此对话框的事件:https://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/v2.0