无法将Facebook的响应传递给其他功能

时间:2013-07-27 11:52:43

标签: javascript facebook function facebook-graph-api

如果您提前感谢您查看此问题!我正致力于从Facebook提取数据。

我正在尝试从Facebook中提取用户名,以便我可以在稍后阶段使用它,我在FB Root div中嵌入了以下代码。

我知道检索工作!但是我无法将其传递给函数returndata我是相对较新的javascript你可以帮帮我吗?我已经尝试了一切

在那里有一个警告来检查它是否正在检索数据

<div id="fb-root"></div>
<script type="text/javascript">

     $(document).ready(function()
     {
        var appId = "121070974711874";

        // If logging in as a Facebook canvas application use this URL.
        var redirectUrl = "http://apps.facebook.com/bggressive";

        // If logging in as a website do this. Be sure to add the host to your           application's App Domain list. 
        var redirectUrl = window.location.href;

        // If the user did not grant the app authorization go ahead and tell them that. Stop code execution.
        if (0 <= window.location.href.indexOf ("error_reason"))
        {
             $(document.body).append ("<p>Authorization denied!</p>");
             return;
        }


       // When the Facebook SDK script has finished loading init the
       // SDK and then get the login status of the user. The status is
       // reported in the handler.
       window.fbAsyncInit = function()
       {
           FB.init({
                     appId : appId,
                     status : true,
                     cookie : true,
                     oauth : true
           });
           FB.getLoginStatus (onCheckLoginStatus);
       };

       (function(d)
       {
          var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
          js = d.createElement('script'); js.id = id; js.async = true;
          js.src = "//connect.facebook.net/en_US/all.js";
          d.getElementsByTagName('head')[0].appendChild(js);
       }(document));


       function onCheckLoginStatus (response)
       {
          if (response.status != "connected")
          {
              top.location.href = "https://www.facebook.com/dialog/oauth?client_id=" + appId + "&redirect_uri=" + encodeURIComponent (redirectUrl) + "&scope=user_photos,friends_photos";
          }
          else
          {
              FB.api('/me', function(response) 
              {
                  PASON = response.username
                  alert(response.username)
              });
          }
       }
   });

   function returndata()
   {
      get = PASON
      return get
   };

</script>

1 个答案:

答案 0 :(得分:1)

我认为你在任何你不能做的随机点调用函数returndata()。原因是 - 变量PASON是异步赋值。因此,您必须以这样的方式编码,即在分配值后调用returndata()

对于您正在尝试使用函数returndata()进行的操作并不十分清楚。但是,我希望你的概念现在很清楚。