eliwedel
eliwedel

Reputation: 1393

Liking a Photo using FB API and JQuery

I am having a problem trying to 'like' a photo using JQuery. I've tried .post, .ajax, and now using the FB.api post. When I run the script I don't even get the error messages. This is what I have as a function when the 'like' button is clicked:

function like(postId) {
    FB.api('/'+postId+'/likes', 'post', function(result) {
        if (!result) {
            alert('Error: No Response');
        } else if (result.error) {
            alert('Error: '+result.error.message+'');
        } else {
            if (result==true) {
                $('#likeButton-'+postId).hide();
                $('#unlikeButton-'+postId).show();
            }
        }
    });
}

Any suggestions would be a great help as I've been searching through the FB api docs for hours!!

Upvotes: 0

Views: 2307

Answers (2)

eliwedel
eliwedel

Reputation: 1393

I figured it out, I had to write the function to be defined at parse-time rather than run-time(as the question is formatted)

like = function(postId) {
    FB.api('/'+postId+'/likes', 'post', function(result) {
        if (!result) {
            alert('Error: No Response');
        } else if (result.error) {
            alert('Error: '+result.error.message+'');
        } else {
            if (result==true) {
                $('#likeButton-'+postId).hide();
                $('#unlikeButton-'+postId).show();
            }
        }
    });
}

Upvotes: 1

DMCS
DMCS

Reputation: 31880

Try this:

function like(postId) {
    FB.api('/'+postId+'/likes', 'post', function(result) {
        console.dir(results);
    });
}

And check your browser's console for the complete object and let us know what you see.

Also use your browser's network sniffer to check to see the Response content of the api call.

Upvotes: 0

Related Questions