简体   繁体   中英

Javascript: like Facebook posts using facebook javascript sdk?

I'm fairly new in using Facebook javascript SDK and I have no idea what I am doing!

I'm trying to like a facebook post using Facebook javascript SDk in my html page.

My full code this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
 <script src='http://connect.facebook.net/en_US/all.js'></script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>


<div  id="bod" style="width:100%; height:100px; background:#000;"></div>

<script>
$( "#bod" ).click(function() {


FB.api("/10153XXXXXXXXXX/likes", 'post',function(response) {
    if(response === true) {
         alert("done!");
    }
});
});
</script>
</body>
</html>

I thought this code will like the given post (its ID is 10153XXXXXXXXXX) once i click on the div #bod but when I click on the div, nothing happens and I don't even get the alert(); at all!

could someone please advise on this issue and let me know what I'm doing wrong?

First of all you'll need to initialize the FB SDK.

Then, in order to call the API you'll need a valid access token therefore, you'll need to login the user using your App.

I would suggest to double check the docs again: https://developers.facebook.com/docs/javascript https://developers.facebook.com/docs/javascript/examples

In the second link you'll find an example to login a user and then call the API that I believe will be helpful for you.

I hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM