从文档开始,如果您将操作附加到 DIV,您还必须grecaptcha.execute();
从 JavaScript 调用,以便执行 reCAPTCHA。假设您有这样的表格:
<!-- CAPTCHA -->
<script src="https://www.google.com/recaptcha/api.js"></script>
<div class="g-recaptcha"
data-sitekey="6LchqW0UAAAAANOoHruD0Ql5aNJIZld4EwLiaf-W"
data-callback="capchaDone" data-size="invisible">
</div>
<form id="loginForm">
<input type="text" name="username" id="usernameField" />
<input type="password" name="password" id="passwordField" />
<input type="submit" />
</form>
并附加一个提交事件监听器,你必须在那里调用 reCAPTCHA:
document.querySelector("#loginForm").addEventListener("submit", function() {
/** Validate input **/
grecaptcha.execute(); // hand execution off to `data-callback`
});
data-callback
验证响应后,您可以在函数中提交表单。
function capchaDone(response) {
console.log(response)
/** Validate reCAPTCHA **/
document.querySelector("#loginForm").submit() // at last, submit the form
}