我使用具有类g-recaptcha
的按钮为每个表单动态加载Invisible reCAPTCHA。
我遇到的问题是验证码没有正确加载,我不知道为什么。我在captcha网站上关注文档,我不知道如何以及为什么会出现此错误:
Uncaught Error: Missing required parameters: sitekey
有人知道问题出在哪里吗?
以下是我使用的代码:
<script src='https://www.google.com/recaptcha/api.js?onload=onloadCallback&hl={{ app.request.locale|default(defaultLang) }}' async defer></script>
JS
var onloadCallback = function () {
$("button.g-recaptcha").each(function () {
var el = $(this);
//SITE_KEY is actually hard coded string.
//It is string that google provided. I just remove it for security reasons...
grecaptcha.render($(el).attr("id"), {
"sitekey": SITE_KEY,
"size": "invisible",
"badge": "inline",
"callback": function (token) {
$(el).parent().find(".g-recaptcha-response").val(token);
$(el).closest("form").submit();
}
}, true);
});
$("button.g-recaptcha").click(function(event) {
event.preventDefault();
grecaptcha.execute();
});
};
HTML示例:
<button
type="submit"
id="submitReviewButton"
class="btn btn-lg btn-submit btn--green g-recaptcha"
>
{{ "review.submit_your_review"|trans }}
</button>
答案 0 :(得分:9)
你在这里缺少一个重要的部分。必须显式呈现api小部件。只需将render=explicit
添加到recaptcha api脚本。
<script src='https://www.google.com/recaptcha/api.js?
onload=onloadCallback
&render=explicit
&hl={{app.request.locale|default(defaultLang) }}' async defer>
</script>
阅读Google文档(reCAPTCHA V2 | reCAPTCHA - Explicitly render the reCAPTCHA widget)。
答案 1 :(得分:0)
如果将Wordpress与CForm Builder和Google Captcha RECaptcha一起使用时遇到“缺少所需参数:sitekey”的情况,则必须将Recaptcha Site Key放在左侧导航栏中的“ CForm Builder”插件的“全局选项”下。您还需要Google Captcha插件中的相同信息。这看起来似乎很明显,但是我很长一段时间都错过了CForm“全局选项”。
答案 2 :(得分:0)
如果您仅在这里使用Recaptcha Invisible v2代码示例:
1.在表单按钮上放置id =“ recaptcha”
2.添加JavaScript
var recaptchaCallback = function() {
$("button#recaptcha").each(function () {
var el = $(this);
grecaptcha.render($(el).attr("id"), {
"sitekey": 'YOUR_GOOGLE_RECAPTCHA_KEY',
"size": "invisible",
"badge": "bottomleft",
"callback": function (token) {
$(el).closest("form").submit();
}
});
});
};
</script>
<script src="https://www.google.com/recaptcha/api.js?onload=recaptchaCallback&render=explicit" async defer></script>