dpa55065 2013-07-30 04:44
浏览 43
已采纳

删除多页重新加载问题

I am trying to fetch the user favourite movies and other details and FB JS SDK. Following works correctly for me. I have tried to use ajax to remove page reload. But I dont find why page gets reloaded twice before Login button appear. And after user enters login credentials and click on login button, before data appear page reloaded twice. I want to remove this mutliple page reload. Can someone please review code and make is easier?

Login.php:

<?php
session_start();
$appId = '66'; 
$appSecret = 'sdsds3'; // Facebook App Secret
$return_url = 'http://yoursite.com/connect_script/';  //path to script folder
$fbPermissions = 'publish_stream,email'; //more permissions : https://developers.facebook.com/docs/authentication/permissions/

?>
<!DOCTYPE html>
<html xmlns:fb="http://www.facebook.com/2008/fbml" xml:lang="en-gb" lang="en-gb" ><head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<title>JS/Ajax Facebook Connect</title>
 <script>
 function AjaxResponse()
 {
    var myData = 'connect=1'; 
     $.ajax({
    type: "POST",
    url: "process_facebook.php",
    data: myData
    }).done(function(result) {
    $("#fb-root").html(result);
    });
   }

function LodingAnimate() //Show loading Image
{
    $("#LoginButton").hide(); //hide login button once user authorize the application
}
 </script></head><body>
<?php
if(!isset($_SESSION['logged_in']))
{
?>
    <div id="results">    </div>
    <div id="LoginButton">
    <div class="fb-login-button" onlogin="javascript:CallAfterLogin();" size="medium" scope="<?php echo $fbPermissions; ?>">Connect With Facebook</div>
    </div>
<?php
}
?>
<div id="fb-root"></div>
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
    appId: '<?php echo $appId; ?>',
    cookie: true,
    xfbml: true,
    channelUrl: '<?php echo $return_url; ?>channel.php',
    oauth: true});};

(function() {
    var e = document.createElement('script');
    e.async = true;
    e.src = document.location.protocol +'//connect.facebook.net/en_US/all.js';
    document.getElementById('fb-root').appendChild(e);}());

function CallAfterLogin(){
        FB.login(function(response) {
        if (response.status === "connected")
        {
            LodingAnimate(); //Animate login
            FB.api('/me', function(data) {
//            $("#fb-root").append(data.);
            console.log(data);
              if(data.email == null)
              {
                    //Facbeook user email is empty, you can check something like this.
                  alert("You must allow us to access your email id!");
                    ResetAnimate();

              }else{
                    AjaxResponse();
              } }); 
         }     });  }
</script>
</body>
</html>

process_facebook.php:

<?php
session_start();
$appId = '66549'; //Facebook App ID
$appSecret = '3944'; // Facebook App Secret
$return_url = 'http://yoursite.com/connect_script/';  //path to script folder
//$fbPermissions = 'publish_stream,email'; //more permissions : https://developers.facebook.com/docs/authentication/permissions/

if(isset($_POST["connect"]) && $_POST["connect"]==1)
{
    if (!class_exists('FacebookApiException')) {
    require_once('facebook.php' );
    }
        $facebook = new Facebook(array(
        'appId' => $appId,
        'secret' => $appSecret,
    ));
    $fbuser = $facebook->getUser();
 if ($fbuser) {
        try {
         $user_interest = $facebook->api('/me/movies');
        //print user facebook data
        echo '<pre>';
        //print_r($user_interest);
        foreach ($user_interest['data'] as $var)
    {
         $ID=$var['id'];
         $Name=$var['name'];
         echo '<br/>';
         print_r($Name); echo '<br/>';
         print_r($ID);

    }
        echo '</pre>';
}
 catch (FacebookApiException $e) {
    echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
    $user = null;
  }}}
?>
  • 写回答

1条回答

  • doubi1928 2013-07-30 08:16
    关注

    Instead of "fb-login-button" with "onlogin" and "scope" attributes, just make a simple button/link/whatever and call "CallAfterLogin" in the "onclick" event. you´re not using FB.login correctly, THAT´s where you have to put in the scope, for example.

    See here: https://developers.facebook.com/docs/reference/javascript/FB.login/

    I´d not call the function "CallAfterLogin" btw, because it´s not called AFTER login, it IS the login code. And you don´t need the process_facebook.php at all, you can just do that with the JavaScript SDK, without using jQuery/Ajax.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料