Unable to pass a response form facebook to another function

限于喜欢 提交于 2020-01-07 02:16:22

问题


If you view this question thank you in advance!, I am working on pulling data from Facebook.

I am trying to pull the username from Facebook so i can use it in a later stage I have embedded the following code in the FB Root div.

I know the retrieve works! however i am not able to pass it on to the function returndata I am relative new to javascript could you please help me out? i have tried everything

There is an alert in there to check if it is retrieving data

<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:


I think you are calling the function returndata() at any random point which you can't do. The reason is- the variable PASON is assigned value asynchronously. So, you have to code in such a way that you call returndata() after the value is assigned!

Its not quite clear what you are trying to do with the function returndata(). But, I hope your concept is clear now.



来源:https://stackoverflow.com/questions/17825385/unable-to-call-a-function-within-a-function-with-a-fb-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!