Facebook应用请求对话框不会出现

时间:2013-07-25 22:21:44

标签: facebook facebook-sdk-3.0

我创建了一个Facebook PHP应用程序,我尝试创建一个请求对话框。我使用了SDK,但是当我点击邀请朋友时它没有出现。

这是代码:

    <?php
// PATH TO THE FB-PHP-SDK
require_once 'src/facebook.php';
$facebook = new Facebook(array(
  'appId'  => '475970839112689',
  'secret' => '4ae240ac95f687af581ff1596d06121d'
));

$user = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl();

if ( empty($user) ) {
    echo("<script> top.location.href='" . $loginUrl . "'</script>");
    exit();
}
?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
etc...

之后,我加载了JS for dialog并创建了一个链接:

    <div id="fb-root"></div>

<script>
window.fbAsyncInit = function() {
    FB.init({
        appId: '475970839161689',
        status: true,
        cookie: true,
        oauth: true
    });
};

$('a').click(sendRequest);
function sendRequest() {
    FB.ui({
        method: 'apprequests',
        message: 'Vino si soializeaza si tu pe Social Chat!',
        title: 'Trimite prietenilor tai o invitatie',
    },
    function (response) {
        if (response.request && response.to) {
            var request_ids = [];
            for(i=0; i<response.to.length; i++) {
                var temp = response.request + '_' + response.to[i];
                request_ids.push(temp);
            }
            var requests = request_ids.join(',');
            $.post('handle_requests.php',{uid: <?php echo $user; ?>, request_ids: requests},function(resp) {
                // callback after storing the requests
            });
        } else {
            alert('canceled');
        }
    });
    return false;
}

  // Load the SDK Asynchronously
(function(d){
 var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
 js = d.createElement('script'); js.id = id; js.async = true;
 js.src = "//connect.facebook.net/ro_RO/all.js";
 d.getElementsByTagName('head')[0].appendChild(js);
}(document));
</script>

<a href="#">Invite friends</a>

因此,当我点击邀请朋友链接时,它什么也没做。

1 个答案:

答案 0 :(得分:0)

将您的点击功能包装在就绪功能中,如下所示:

$(document).ready(function() { 
   $('a').click(sendRequest);
});