javascript 不可见的 reCAPTCHA - 缺少必需的参数:sitekey
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49174555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Invisible reCAPTCHA - Missing required parameters: sitekey
提问by jureispro
I am loading Invisible reCAPTCHA dynamically for every form with button that has class g-recaptcha.
我正在为每个带有 class 的按钮的表单动态加载 Invisible reCAPTCHA g-recaptcha。
Problem that I have is that captcha is not loading correctly and I am not sure why. I followed documentation on captcha website and I am not sure how and why I got this error:
我遇到的问题是验证码加载不正确,我不知道为什么。我遵循了验证码网站上的文档,但不确定如何以及为什么会出现此错误:
Uncaught Error: Missing required parameters: sitekey
Does someone knows where is the problem?
有人知道问题出在哪里吗?
Here is code I use:
这是我使用的代码:
<script src='https://www.google.com/recaptcha/api.js?onload=onloadCallback&hl={{ app.request.locale|default(defaultLang) }}' async defer></script>
JS
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();
});
};
EXAMPLE OF HTML:
HTML 示例:
<button
type="submit"
id="submitReviewButton"
class="btn btn-lg btn-submit btn--green g-recaptcha"
>
{{ "review.submit_your_review"|trans }}
</button>
回答by Roshana Pitigala
You are missing an important part here. The api widget must rendered explicitly. Just add render=explicitto recaptcha api script.
你在这里错过了一个重要的部分。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>
Read the Google doc (reCAPTCHA V2 | reCAPTCHA - Explicitly render the reCAPTCHA widget).
阅读 Google 文档(reCAPTCHA V2 | reCAPTCHA - 显式呈现 reCAPTCHA 小部件)。
回答by GlenPeterson
If you're getting "Missing required parameters: sitekey" while using Wordpress with CForm Builder and Google Captcha RECaptcha, you have to put the Recaptcha Site Key under "Global Options" in the left-nav for the CForm Builder plugin. You also need the same info in the Google Captcha plugin. This may seem obvious, but I missed the CForm "Global Options" for a long time.
如果在将 Wordpress 与 CForm Builder 和 Google Captcha RECaptcha 一起使用时遇到“缺少所需参数:站点密钥”,则必须将 Recaptcha 站点密钥放在 CForm Builder 插件左侧导航中的“全局选项”下。您还需要 Google Captcha 插件中的相同信息。这可能看起来很明显,但我很长时间都没有看到 CForm 的“全局选项”。
回答by Mantas D
If you are here only for working Recaptcha Invisible v2 code example:
1. Put id="recaptcha" on your form button
2. Add JavaScript
如果您只是为了工作 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>

