5

I have setup a share popup triggered by Jquery. It works, but I want to use the response value to trigger an AJAX call on success and Facebook only returns an empty array.

Here is the javascript


    $("#fb-request").click(function () {
        FB.ui({
            method: 'share',
            name: 'Check out website',
            href: 'URL_TO_SHARE',
        },
        function (response) {
            if (response && !response.error_code) {
              console.log(response); // Returns: []
            }
        });
    });

Because of this I can't make the difference between someone who posts and someone who uses the cancel button. Am I missing something here? Or is there something to setup on the Facebook App?

Thanks,

Imran Qamer
  • 2,253
  • 3
  • 29
  • 52
user1150316
  • 161
  • 3
  • 8

4 Answers4

4

From documentation:

Response Data

Parameter Description object_id Only available if the user is logged into your app using Facebook and has granted publish_actions. If present, this is the ID of the published Open Graph story.

Community
  • 1
  • 1
Daredzik
  • 422
  • 2
  • 9
  • 21
2

If the user cancels the share dialog, the response is undefined, so:

$("#fb-request").click(function () {
    FB.ui({
        method: 'share',
        name: 'Check out website',
        href: 'URL_TO_SHARE',
    },
    function (response) {
        if (response && !response.error_code) {
            if (typeof response != 'undefined'){
                //shered
            }
        }
    });
});
Pang
  • 9,564
  • 146
  • 81
  • 122
Marcin Żurek
  • 145
  • 1
  • 3
  • This code works fine in PC / Android, but it doesn't work on iOS. As long as the user is not logged in to your app (with the proper permission), the value of response will always be an empty array (regardless if the share has happened or not). – Alon Bilu Jul 02 '17 at 14:34
0

Check first if your user is connected.

FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
    FB.ui(
        {
            method: 'feed',
            link: 'your_url',
            mobile_iframe: true
        },
        function(response){
        });
}
else {
    FB.login(function(response){
        FB.ui(
        {
            method: 'feed',
            link: 'your_url',
            mobile_iframe: true
        },
        function(response){
        });
    });
}

});

Simon Berton
  • 468
  • 5
  • 13
0

this worked for me.

FB.ui({
    method: 'share',
    display: 'popup',
    href: url,
}, function(response){
    if (typeof response != 'undefined'){
        alert("shared");
    }else{
        alert("no shared");
    }
});