开发者

facebook - LIKE a post via ajax

I am creating a facebook wall (stream) look-a-like to put on my site. This compone开发者_Go百科nt will read all posts from a specific page`s wall and display them, via the graph api. I also want the user to be able to LIKE the posts displayed on the "wall".

What I have so far is a script that uses the graph api to get the JSON list of posts and I also have a PHP file that can LIKE a post who`s ID is submitted in the post_id query string parameter, and this does work. I see the LIKE is submitted.

To call this PHP file I use jQuery ajax:

 function do_likes(post_id) {
    $.ajax({
        type: "POST",
        url:"http://www.p-art.co.il/facebook_test/action.php?post_id=" + post_id
    });

Firebug doesn't show any error, but on the other hand, the LIKE is not posted. I have been searching for several hours, but I can't find the correct way to call the PHP file, in order for the FB.api call to work.

Thank you in advance. -Elad


With a HTTP POST, data is normally sent from form inputs with the enctype set to application/x-www-form-urlencoded format. So with an AJAX POST, we would usually send data in this format also and not as a query string parameter, which is how data is usually sent with a HTTP GET request and how you are sending data above.

if you change your code to

 function do_likes(post_id) {
    $.ajax({
        type: "POST",
        url:"http://www.p-art.co.il/facebook_test/action.php",
        data : { post_id : post_id }
    });
 }

it should work as expected (I'm not familiar with PHP but I assume that the URL you're posting to expects data in application/x-www-form-urlencoded format). with jQuery.ajax(), if you set the data object to the key/value pairs that you want to send to the server, jQuery will take care of providing the correct enctype for you based on the HTTP request type you are using (you can override the enctype if necessary, but usually this is not required and the defaults will be what you need in the majority of cases).

Also, you may want to set a callback function to be called after the AJAX post has successfully completed. To do this add a success property to the object passed to the $.ajax() call.


It's hard to tell without seeing the source code for your action.php file but I'm guessing its not getting the users access token correctly due to it being called via AJAX.

If you can post your action.php source somewhere I should be able to help some more

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜