最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

facebook - Javascript FB.api - undefined error - Stack Overflow

programmeradmin0浏览0评论

I am trying to use JS interface for facebook api and it is my first application for it. Here is a snippet:

HTML:

    <div id="fb-root"></div>
    <script src=".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=".js"></script>

I have 'Your name is undefined' when I load this page. But this code

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

works as expected. Could you please help me? Thanks!

I am trying to use JS interface for facebook api and it is my first application for it. Here is a snippet:

HTML:

    <div id="fb-root"></div>
    <script src="https://connect.facebook/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/ru_RU/all.js"></script>

I have 'Your name is undefined' when I load this page. But this code

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

works as expected. Could you please help me? Thanks!

Share Improve this question asked Feb 9, 2012 at 20:40 dbfdbf 6,5192 gold badges42 silver badges69 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

If you log your response variable via console.log(response) you'll see what's wrong: You'll get an error object with this message:

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

So if you want to get information about the current user you have to send also an Access Token. To get more information about this check the Facebook JS SDK page.

You need to make sure that the user has logged into Facebook and authorized your app, before you call FB.api('/me', ...).

Here's the general information: http://developers.facebook./docs/guides/web/#login

To overe with Undefined problem use the following code:

  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);
});

  } 
 });
发布评论

评论列表(0)

  1. 暂无评论