6

在我的代碼信息,我有Facebook上的JavaScript SDK中的接入令牌必須用於查詢當前用戶

FB.api('/me/friends', function(response) { 
      if(response.data) { 
       //TODO : what to do if no. of friends is more than 5000 (pagination by fb) 
       friends_data=response.data; 
       dijit.registry.byId("mainWidget_div").set_friends_data(friends_data); 
      } else { 
       alert("Error!"); 
      } 

      }); 

這給出了一個錯誤。 ,如果我手動調用此函數(在控制檯上),有沒有錯誤

FB.api('/me/friends', function(response){r=response;}); 
//wait a while 
r 

現在r.data是我朋友的數組。

我檢查了網絡面板,當我手動調用它時,訪問令牌會自動插入到請求url中,並且當它通過代碼調用時,訪問令牌不會被插入。

在我的應用程序的完整FB SDK加載代碼是這樣的:

<script type="text/javascript"> 
     // You probably don't want to use globals, but this is just example code 
     var fbAppId = "{{facebook_app_id}}"; 

     // This is boilerplate code that is used to initialize the Facebook 
     // JS SDK. You would normally set your App ID in this code. 

     // Additional JS functions here 
     window.fbAsyncInit = function() { 
     FB.init({ 
      appId  : fbAppId,  // App ID 
      status  : true,   // check login status 
      cookie  : true,   // enable cookies to allow the server to access the session 
      xfbml  : true   // parse page for xfbml or html5 social plugins like login button below 
     }); 

     // Put additional init code here 
     dojo.ready(function(){ 
      FB.api('/me/friends', function(response) { 
      if(response.data) { 
       //TODO : what to do if no. of friends is more than 5000 (pagination by fb) 
       friends_data=response.data; 
       dijit.registry.byId("mainWidget_div").set_friends_data(friends_data); 
      } else { 
       alert("Error!"); 
      } 

      }); 
     }); 
     }; 
     // Load the SDK Asynchronously 
     (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/en_US/all.js"; 
     fjs.parentNode.insertBefore(js, fjs); 
     }(document, 'script', 'facebook-jssdk')); 

    </script> 

回答

2

我的猜測是,你正在試圖讓好友列表Facebook的API完全初始化前。你看到什麼錯誤?

您正在註冊將在DOM就緒(dojo.ready)上運行的FB.api調用。這可能會導致它加載不同步,即使它全部包裝在fbAsyncInit中。朋友API調用本身不依賴於DOM,因此我不會將它封裝在dojo調用中。你沒有在控制檯中這樣做,它的工作。

我不是JavaScript的專家。如果我做出了一個可能不正確的猜測,那麼發生這種情況的原因可能與javascript提升有關。

+0

謝謝!你已經引導我回答,儘管它與你的猜測不完全相同。 – Eva 2014-02-24 01:40:28

4

answer from Brent Baisleyanother answer到一個不同的問題,幫我找出了什麼是錯的。

您不能在FB.init()之後立即調用FB.init()依賴方法,因爲它是異步加載的。即使像dojo.ready()那樣異步加載數據也無濟於事。您必須將代碼包裝在FB.getLoginStatus()中。

相關問題