问题描述
我正在尝试将集成到我的网络应用程序中。
我的页面有 Google 按钮:
ModalDialogue.cshtml:
<a class="google g-signin2" data-onsuccess="onSignIn">Sign up with Google+</a>
我正在尝试在我的 js 中触发该方法:
ModalDialogue.js:
function onSignIn(googleUser) {
var profile = googleUser.getBasicProfile();
console.log('ID: ' + profile.getId()); // Do not send to your backend! Use an ID token instead.
console.log('Name: ' + profile.getName());
console.log('Image URL: ' + profile.getImageUrl());
console.log('Email: ' + profile.getEmail());
}
我知道
a] JavaScript 在页面的范围内,因为我的其他功能 - 用于打开和关闭对话 - 可以工作。
b] 该按钮正在与 Google 通信,因为点击它会打开“使用 Google 登录”对话框 - 然后一旦完成 - 之后将按钮更改为“登录”。
如何触发 onSignIn 方法? 或者至少我如何确定是否触发了 onsuccess ?
1楼
DaveC426913
5
2015-08-04 15:07:42
哦。 我的错。 我没有注意到我将方法包裹在闭包中。 需要全球化。
2楼
bradlis7
3
2016-06-16 04:07:54
对于以后来这个页面的人,我不得不将函数定义移到按钮标签上方。 似乎这无关紧要,因为 data-onsuccess 只是一个字符串。 不幸的是,它没有抛出任何错误消息。
3楼
Prakash Choudhary
0
2019-04-08 13:48:50
<html lang="en">
<head>
<meta name="google-signin-scope" content="profile email">
<meta name="google-signin-client_id" content="YOUR_CLIENT_ID.apps.googleusercontent.com">
<script src="https://apis.google.com/js/platform.js" async defer></script>
</head>
<body>
<div class="g-signin2" data-onsuccess="onSignIn" data-theme="dark"></div>
<script>
function onSignIn(googleUser) {
// Useful data for your client-side scripts:
var profile = googleUser.getBasicProfile();
console.log("ID: " + profile.getId()); // Don't send this directly to your server!
console.log('Full Name: ' + profile.getName());
console.log('Given Name: ' + profile.getGivenName());
console.log('Family Name: ' + profile.getFamilyName());
console.log("Image URL: " + profile.getImageUrl());
console.log("Email: " + profile.getEmail());
// The ID token you need to pass to your backend:
var id_token = googleUser.getAuthResponse().id_token;
console.log("ID Token: " + id_token);
}
</script>
</body>
</html>
信息
4楼
Anurag Kumar
0
2020-09-28 10:25:20
在 script 标签中定义 onSignIn 函数,使其全局可用。
<script>
function onSignIn(googleUser) {
console.log("I am clicked");
var profile = googleUser.getBasicProfile();
console.log("ID: " + profile.getId());
console.log("Name: " + profile.getName());
console.log("Image URL: " + profile.getImageUrl());
console.log("Email: " + profile.getEmail());
}
</script>
要检查您的函数是否全局可用,请从控制台调用 window.onSignIn() 或 onSignIn()。 如果它是全局的,您应该能够调用 onSignIn。
script 标签可以放在 body 标签的底部(用 React js 测试),
<body>
....
<script>
...
</script></body>
或者,它可以使用 async 或 defer 属性放在 head 内。
这将发挥它的魔力,
<div className="g-signin2" data-onsuccess="onSignIn"></div>
祝你好运。
5楼
Subham choudhary
-1
2019-11-21 18:14:23
在您的浏览器中启用第三方 cookie