使用 Javascript FB.getLoginStatus 的 Facebook 登录在一种情况下有效,而在花药中无效。
这有效:
//view
<script type="text/javascript">
function f1() {
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// the user is logged in and has authenticated 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
FB.api('/me', function(response) {
alert(response.name + " " + response.id);
name1 = response.name;
uid1 = response.id;
});
// var uid = response.authResponse.userID;
// var accessToken = response.authResponse.accessToken;
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
} else {
// the user isn't logged in to Facebook.
}
});
}
</script>
<%= content_for :body_attributes, "onload='f1();'" %>
这不起作用
// view
<script type="text/javascript">
function f1() {
// same definition as above
}
</script>
<script type="text/javascript"> {
f1()
}
</script>
我不明白为什么第二个代码不起作用。谢谢!