Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用facebook全球响应?_Javascript_Jquery_Facebook - Fatal编程技术网

Javascript 如何使用facebook全球响应?

Javascript 如何使用facebook全球响应?,javascript,jquery,facebook,Javascript,Jquery,Facebook,如何在另一个函数中获取用户id并将其用作全局变量 function get_id() { FB.getLoginStatus(function(response) { if (response.status === 'connected') { // the user is logged in and connected to your // app, and response.authResponse supplies

如何在另一个函数中获取用户id并将其用作全局变量

    function get_id() {
        FB.getLoginStatus(function(response) {
      if (response.status === 'connected') {
        // the user is logged in and connected to your
        // app, and response.authResponse supplies
        // the user’s ID, a valid access token, a signed
        // request, and the time the access token 
        // and signed request each expire
        uid = response.authResponse.userID;
        accessToken = response.authResponse.accessToken;
      } else if (response.status === 'not_authorized') {
        // the user is logged in to Facebook, 
        //but not connected to the app
      } else {
        // the user isn't even logged in to Facebook.
      }
    });
    }

fb_user_id = get_id();

JavaScript本质上是异步的。您必须回调响应函数中的另一个函数。

您最好在函数中设置此变量,而不是通过调用:

(function(w) {
    FB.getLoginStatus(function(response) {
        if (response.status === 'connected') {
            // the user is logged in and connected to your
            // app, and response.authResponse supplies
            // the user’s ID, a valid access token, a signed
            // request, and the time the access token 
            // and signed request each expire

            w.fb_user_id = response.authResponse.userID;
            w.accessToken = response.authResponse.accessToken;
        } else if (response.status === 'not_authorized') {
            // the user is logged in to Facebook, 
            //but not connected to the app
        } else {
            // the user isn't even logged in to Facebook.
        }
    });
})(window);
然后稍后在页面中(如果调用不立即依赖于此变量),您可以访问页面上任何位置的变量作为全局变量
fb\u user\u id
(也称为
window.fb\u user\u id

如果需要在用户ID准备好后立即运行代码,则需要使用回调。如果使用jQuery 1.5,还可以使用jQuery延迟来帮助解决同步性问题:

var getUserId = function () {
    return $.Deferred(function(d) {
        FB.getLoginStatus(function(response) {
            if (response.status === 'connected') {
                // the user is logged in and connected to your
                // app, and response.authResponse supplies
                // the user’s ID, a valid access token, a signed
                // request, and the time the access token 
                // and signed request each expire
                var auth = response.authResponse;
                d.resolve(auth.userID, auth.accessToken);
            } else if (response.status === 'not_authorized') {
                // the user is logged in to Facebook, 
                //but not connected to the app

                d.reject();
            } else {
                // the user isn't even logged in to Facebook.

                d.reject();
            }
        });

    }).promise();   
};

getUserId()
    .done(function(userId, token) {
        // userId and token are available in here
    })
    .fail(function() {
        // run some code in here if not authorized
        // or something else failed;
    });

你能举个例子吗?我是JS的新手,那么我怎样才能在这个函数之外使用w.fb_user_id?我无法访问。我尝试了“console.log(fb_user_id);”我得到了一个未经证实的错误。这是一个facebook应用程序,在tab页面中工作。这会影响任何事情吗?就像我说的,使用这种方法,你将无法立即访问用户ID,因为它是异步设置的。我将为另一种方法提供回调机制。函数(userId,token){function(blah){function(blah2){console.log(userId);}}