title: Facebook API & Ajax POST request
body: <p>I have a Facebook connect/disconnect link. When user clicks on "Facebook connect" link (on <strong>index.php</strong>), it checks the user state (Is he connected to Facebook or not?) then if he's not connected, my script sends an Ajax $.post request to <strong>connect.php</strong> in order to retrieve infos from database (for instance : Is the user registered in our database?). After that, it sends back a response to index.php : if there is no error, the page is refreshed. In this case, if everything is ok, after the page is refreshed by the script (<strong>window.location.reload();</strong>), we should see the Facebook user ID (UID).</p>
  
  <p>The problem is the UID is still empty after the page is reloaded through the ajax callback. But, if I refresh the page manually one more time, I can now see the UID.</p>
  
  <p>I test many ways to understand where the problem comes from, and I found there is a problem with this Facebook request : <strong>$me = $facebook->api('/me');</strong></p>
  
  <p>Anyway, I can't do without <strong>$me = $facebook->api('/me');</strong> so if you want to test my script and find the problem, here is what you need ! (don't forget the last Facebook SDK) ;-)</p>
  
  <p>Any help would be very apreciated ! Thanks !</p>
  
  <p><strong>index.php</strong></p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
  &lt;title&gt;Facebook Connect&lt;/title&gt;
  &lt;script src="js/jquery.js"&gt;&lt;/script&gt;
  &lt;?php
  
  /* FACEBOOK APP CONFIGURATION */
  $appId = 'YOU_APP_ID';
  $appSecret = 'YOUR_APP_SECRET';
  
  define("APPID",$appId);
  define("APPSECRET",$appSecret);
  
  /* API CALL */
  if (!class_exists('FacebookApiException'))
  {
    require_once('inc/facebook.php' );
  }
  
  $facebook = new Facebook(array(
     'appId' =&gt; APPID,
     'secret' =&gt; APPSECRET,
  ));
  
  $fb_user = $facebook-&gt;getUser();
  
  if ($fb_user) {
      try
      {
         $me = $facebook-&gt;api('/me');
         $uid = $facebook-&gt;getUser();
      }
        catch (FacebookApiException $e)
      {
         //echo error_log($e);
         $fb_user = null;
      }
  }
  ?&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  &lt;div id="fb-root"&gt;&lt;/div&gt;
  &lt;script&gt;(function(d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id;
    js.src = "//connect.facebook.net/fr_FR/all.js#xfbml=1&amp;appId=&lt;?=APPID?&gt;";
    fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'facebook-jssdk'));&lt;/script&gt;
  
  &lt;script type="text/javascript"&gt;
      window.fbAsyncInit = function() {
          FB.init({
              appId   : '&lt;?=APPID?&gt;',
              oauth   : true,
              status  : true,
              cookie  : true,
              xfbml   : true
          });
      };
  
      function fb_connect() {
          alert('FB.getLoginStatus');
          FB.getLoginStatus(function(response) {
              if (response.status === 'connected') {
                  var access_token = response.authResponse.accessToken;
                  var user_id = response.authResponse.userID;
                  //
                  window.location.reload();
                  //
              } else {
                  alert('FB.login');
                  FB.login(function(response) {
                      var access_token = response.authResponse.accessToken;
                      var user_id = response.authResponse.userID;
                          $.post('connect.php', function(data) {
                          var obj = $.parseJSON(data);
                          if (obj['error']==0) {
                              alert(obj['message']);
                              window.location.reload();
                          } else {
                              alert(obj['message']);
                          }
                      });
                      //
  
                  }, {
                      scope: 'email, publish_stream, user_birthday'
                  });
              }
          });
      }
  
      function fb_logout() {
          FB.logout(function(response) {
              window.location.reload();
          }); 
      }
  
      (function() {
        var e = document.createElement('script');
        e.src = document.location.protocol + '//connect.facebook.net/fr_FR/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
      }());
  &lt;/script&gt;
  &lt;p&gt;UID : &lt;?=$fb_user?&gt;&lt;/p&gt;
  &lt;?php if ($fb_user) { ?&gt;
  &lt;a href="#" onclick="fb_logout();"&gt;Disconnect&lt;/a&gt;
  &lt;?php } else { ?&gt;
  &lt;a href="#" onclick="fb_connect();"&gt;Facebook connect&lt;/a&gt;
  &lt;?php } ?&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>connect.php</strong></p>
  
  <pre><code>&lt;?php
  /* FACEBOOK APP CONFIGURATION */
  $appId = 'YOUR_APP_ID';
  $appSecret = 'YOUR_APP_SECRET';
  
  define("APPID",$appId);
  define("APPSECRET",$appSecret);
  
  /* API CALL */
  if (!class_exists('FacebookApiException'))
  {
    require_once('inc/facebook.php' );
  }
  
  $facebook = new Facebook(array(
     'appId' =&gt; APPID,
     'secret' =&gt; APPSECRET,
  ));
  
  $fb_user = $facebook-&gt;getUser();
  
  if ($fb_user) {
      try
      {
         $me = $facebook-&gt;api('/me');
         $uid = $facebook-&gt;getUser();
      }
        catch (FacebookApiException $e)
      {
         //echo error_log($e);
         $fb_user = null;
      }
  }
  
  /* CALLBACK */
  $result = array();
  
  if ($fb_user) {
      $result['error'] = 0;
      $result['message'] = $me['first_name'];;
  } else {
      $result['error'] = 1;
      $result['message'] = 'error';
  }
  
  echo json_encode($result);
  ?&gt;
  </code></pre>