Andrew
Andrew

Reputation: 421

Facebook Event.subscribe comments.create not working

Can anyone tell me why this isn't working for me?

Example page here

http://totalcommunitycollegemove.com/post-438.html

window.fbAsyncInit = function() {   
    FB.init({appId: '194189377275548', status: true, cookie: true,
         xfbml: true});

};
(function() {
    var e = document.createElement('script'); e.async = true;
    e.src = document.location.protocol +
      '//connect.facebook.net/en_US/all.js';
    document.getElementById('fb-root').appendChild(e);
}());

function catchCommentAdd()
{
    $.ajax({
        type: "POST",
        url: "ajaxCommentCount.php",
        data: "id=<?php echo $_GET['id']; ?>&direction=up",
        dataType: "json"
    });
    return true;
}

function catchCommentDelete()
{     
    $.ajax({
        type: "POST",
        url: "ajaxCommentCount.php",
        data: "id=<?php echo $_GET['id']; ?>&direction=down",
        dataType: "json"
    });

    return true;
}

FB.Event.subscribe('comments.create', function(response) { 
    alert(response);
    catchCommentAdd();
});

Upvotes: 1

Views: 6619

Answers (4)

Randoogle
Randoogle

Reputation: 151

Use "comment.create" and not "comments.create" it's singular.

Upvotes: 2

Fabrice
Fabrice

Reputation: 3100

You read Facebook's documentation? That's a big mistake unfortunately.
Try with comments.add instead of comments.create...

Upvotes: 0

schnickers
schnickers

Reputation: 94

i had the same problem but maybe found a solution (works on my test-site). It seems that the event name "comments.create" is wrong. The correct (or at least working) event is "comment.create" (without the s)

Here is my code-snippet:

<script>
    window.fbAsyncInit = function() {
        FB.init({
            appId:  'APP_ID',
            status: true,
            cookie: true,
            xfbml:  true
        });

        FB.Event.subscribe('comment.create', function(response) {
            alert(response.commentID);
        });         

    };
    (function() {
        var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);
    }());
</script>
<fb:comments href="YOUR_URL" num_posts="5" width="500" ></fb:comments>

Upvotes: 4

Ben Regenspan
Ben Regenspan

Reputation: 10548

This code:

FB.Event.subscribe('comments.create',
function(response) { 
    alert(response);
    catchCommentAdd(); });

Relies on Facebook's Javascript SDK having already loaded in. Because that happens asynchronously, you should place the code in your window.fbAsyncInit function, following the FB.init call:

window.fbAsyncInit = function() {   
    FB.init({appId: '194189377275548', status: true, cookie: true,
         xfbml: true});

    FB.Event.subscribe('comments.create', function(response) { 
        alert(response);
        catchCommentAdd();
     });
};

Upvotes: 0

Related Questions