“Request Dialog” requestCallback when clicking Cancel or Close button

我只是一个虾纸丫 提交于 2019-12-08 06:45:17

问题


I am new into Facebook application development and also newbie about JavaScript and PHP programming.

I currently developing a Facebook application but currently stuck with Request Dialog window.

When the Request Dialog appears, I choose friends I want and then click "Send Requests", the requestCallback(response) are executed and friends who getting requests are notified as expected. But, If I click "Cancel" or the blue-colored close button, the requestCallback(response) is also executed but selected friends are not getting notified about the request.

Here is my code:

function requestCallback(response)
{
    //console.log(response);
    location.href='step2.php';
}

So, whether I click "Cancel" or close button, the script above are still executed (moving to page step2.php I specify.)

What I want is when the user clicking cancel button or close modal window button, the page stay at the same page.

Anyone know how to solve this problem?

Thanks!


回答1:


You can just check what's inside the Facebook response object, because it won't be the same if requests have been sent or not !

Something like :

function requestCallback(response)
{
    if(response && response.request_ids) {
         // Here, requests have been sent, facebook gives you the ids of all requests
         //console.log(response);
         location.href='step2.php';
    } else {
         // No requests sent, you can do what you want (like...nothing, and stay on the page).
    }
}

Or if you are using the new structure (Request 2.0 Efficient):

function requestCallback(response)
{
    if(response && response.request) {
         // Here, requests have been sent, facebook gives you the request and the array of recipients
         //console.log(response);
         location.href='step2.php';
    } else {
         // No requests sent, you can do what you want (like...nothing, and stay on the page).
    }
}

Look at the structure of the response object to make your condition. The callback is fired even when you hit close in order to have the possibility to notice when your user quits the dialog. It's up to you to verify if he sent requests, and act like you want ! :)

Also, something important : Facebook updated their request system a few weeks ago, making available "Requests 2.0" in your apps settings. It's turned off by default, but if you activate it, the structure of the response object when sending requests to people will change. So you'd have to update your condition in the callback !

Everything is explained here : http://developers.facebook.com/blog/post/569/



来源:https://stackoverflow.com/questions/7765946/request-dialog-requestcallback-when-clicking-cancel-or-close-button

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