Liking a Photo using FB API and JQuery

回眸只為那壹抹淺笑 提交于 2019-12-08 12:46:27

问题


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!!


回答1:


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();
            }
        }
    });
}



回答2:


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.



来源:https://stackoverflow.com/questions/8921870/liking-a-photo-using-fb-api-and-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!