我正在尝试在 WordPress 网站中以 HTML 形式实现 Google 的 Invisible reCAPTCHA。
In the head
首先,我有一个设置回调并将表单的提交事件绑定到验证的脚本:
jQuery(document).ready(function() {
var valid = false;
window.recaptchaOkay = function(token) {
valid = true;
jQuery('#cadastro').submit();
};
document.getElementById('cadastro').addEventListener('submit', function validate(event) {
if (valid) {
return true;
}
var cap = document
.getElementById('cadastro')
.querySelector('.g-recaptcha');
grecaptcha.execute(cap);
event.preventDefault();
return false;
});
});
然后,我加载 reCAPTCHA 脚本,正如文档中所示:
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
In the body
这是我正在使用的表格:
<form action="https://example.com/" method="post" id="cadastro">
<div
class="g-recaptcha"
data-sitekey="6Lc0jC4UAAAAANlXbqGWNlwyW_e1kEB89zLTfMer"
data-callback="recaptchaOkay"
data-size="invisible"
id="cadastro-captcha">
</div>
<button type="submit" id="cadastro-submit">Enviar</button>
</form>
会发生什么
我填写表单,提交它,然后抛出以下错误(在与grecaptcha.execute
):
Error: Invalid ReCAPTCHA client id: [object HTMLDivElement]
也尝试过只是通过cadastro-captcha
作为字符串直接指向该函数的 ID(例如grecaptcha.execute("cadastro-captcha")
),但同样的错误发生(显然 id 不同)。同样,如果我不传递任何参数,也会发生相同的错误,除了它引用的是undefined
.