gpt4 book ai didi

javascript - Google Invisible ReCaptcha 不可见

转载 作者:搜寻专家 更新时间:2023-11-01 04:29:24 25 4
gpt4 key购买 nike

我只是尝试在提交表单后让 Google Invisible ReCaptcha 工作。我的问题是,ReCaptcha 不是不可见的,它看起来像“旧的”recaptcha 正在弹出。我不明白为什么。我的站点 key 用于不可见的重新验证。请帮助我。

首先我正在加载 API:

<script src='https://www.google.com/recaptcha/api.js?render=explicit&onload=onScriptLoad' async defer></script>

我的表格是这样的:

<form method="post" id="contact-form-face" class="clearfix" onsubmit="return false">
<input type="text" required name="name" value="name" onFocus="if (this.value == 'name') this.value = '';" onBlur="if (this.value == '') this.value = 'name';" />
<button type="submit" id="sendButton" data-size="invisible" class="g-recaptcha contact_btn" onclick="onSubmitBtnClick()" value="send" >send</button>
</form>

JS:

window.onScriptLoad = function () {
// this callback will be called by recapcha/api.js once its loaded. If we used
// render=explicit as param in script src, then we can explicitly render reCaptcha at this point

// element to "render" invisible captcha in
var htmlEl = document.querySelector('.g-recaptcha');

// option to captcha
var captchaOptions = {
sitekey: 'XXXXXXX',
size: 'invisible',
// tell reCaptcha which callback to notify when user is successfully verified.
// if this value is string, then it must be name of function accessible via window['nameOfFunc'],
// and passing string is equivalent to specifying data-callback='nameOfFunc', but it can be
// reference to an actual function
callback: window.onUserVerified
};

// Only for "invisible" type. if true, will read value from html-element's data-* attribute if its not passed via captchaOptions
var inheritFromDataAttr = true;

console.log("render now!");
// now render
recaptchaId = window.grecaptcha.render(htmlEl, captchaOptions, inheritFromDataAttr);
};

// this is assigned from "data-callback" or render()'s "options.callback"
window.onUserVerified = function (token) {
alert('User Is verified');
console.log('token=', token);

};

// click handler for form's submit button
function onSubmitBtnClick () {
var token = window.grecaptcha.getResponse(recaptchaId);

// if no token, mean user is not validated yet
if (!token) {
// trigger validation
window.grecaptcha.execute(recaptchaId);
return;
}

var xhrData = {
'g-recaptcha-response': token
// more ajax body/data here
};

};

为了让事情更清楚:这个 reCaptcha 工作正常,回调加载,验证也工作正常......唯一的问题是,这个 recaptcha 必须是不可见的,而它不是:/

最佳答案

我认为你的问题是你显式地调用了 render() 方法

recaptchaId = window.grecaptcha.render(htmlEl, captchaOptions, inheritFromDataAttr);

};

如果你想让它不可见,你必须包含这个自定义DIV

    <div class="g-recaptcha"
data-sitekey="your_site_key"
data-callback="onSubmit"
data-size="invisible">
</div>

https://developers.google.com/recaptcha/docs/invisible

recaptcha 脚本将查找 div 类元素并将其绑定(bind)在那里,然后在调用 recaptcha 验证时执行回调。

调用 recaptcha 验证使用 grecaptcha.execute();

按照 googledev 页面中的示例,非常简单。

希望对您有所帮助 =)

关于javascript - Google Invisible ReCaptcha 不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43733787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com