2012-02-09 117 views
0

我正在尝试使用JS界面的Facebook API,这是我的第一个应用程序。 这里是一个片段:Javascript FB.api - 未定义的错误

HTML:

<div id="fb-root"></div> 
    <script src="https://connect.facebook.net/ru_RU/all.js"></script> 
    <script type="text/javascript">facebook_init();</script> 

JS:

function facebook_init() { 
    FB.init({ 
     appId  : '<MY APP ID IS HERE>', 
     channelUrl : '/media/channel.html', // Channel File 
     status  : true, // check login status 
     cookie  : true, // enable cookies to allow the server to access the session 
     xfbml  : true // parse XFBML 
    }); 
    FB.api('/me', function(response) { 
       alert('Your name is ' + response.name); 
      }); 
} 

channel.html:

<script src="https://connect.facebook.net/ru_RU/all.js"></script> 

我 '你的名字是未定义' 当我加载这一页。 但是,此代码

FB.ui({ method: 'apprequests', 
     message: 'MSG', 
     title: 'TITLE'}); 

按预期工作。 你能帮我吗? 谢谢!

回答

1

如果通过console.log(response)you'll see what's wrong登录您的response变量: 你会得到这个消息的错误对象:

"An active access token must be used to query information about the current user." 

所以,如果你想获得当前用户的信息,你也必须发送一个访问令牌。要获得更多信息,请查看Facebook JS SDK页面。

0

要与Undefined问题克服使用下面的代码:

window.fbAsyncInit = function() { 
// init the FB JS SDK 
FB.init({ 
    appId  : '<APP ID>',       
    status  : true,         
    xfbml  : true         
}); 

// Additional initialization code such as adding Event Listeners goes here 

FB.getLoginStatus(function(response) { 
    if (response.status === 'connected') { 
alert("connected"); 
connecter=true; 
FB.api('/me', function(user) { 
alert(user.name); 
alert(user.first_name); 
alert(user.last_name); 
alert(user.email); 
}); 

    } 
});