Javascript 未捕获的 ReferenceError:grecaptcha 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29822607/
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
Uncaught ReferenceError: grecaptcha is not defined
提问by codeofnode
I am using recaptchav2
我正在使用recaptchav2
I am getting the following error occasionally (sometimes i don't get error and sometimes i do get it)
我偶尔会收到以下错误(有时我没有收到错误,有时我确实收到了)
Uncaught ReferenceError: grecaptcha is not defined
It seems because of the internal http request. that takes some time to get another js recaptcha__en.js. Meanwhile actual rendering code of grecaptchaexecutes.
似乎是因为内部 http 请求。这需要一些时间来获得另一个 js recaptcha__en.js。同时grecaptcha执行的实际渲染代码。
So what is the standard solution to avoid this issue.?
那么避免这个问题的标准解决方案是什么?
PS : of course i am looking for some solution other than setTimeout
PS:当然,我正在寻找除此之外的其他解决方案 setTimeout
采纳答案by Vigneswaran Marimuthu
Recaptcha has a onloadcallback that will run once recaptcha is loaded. Place your code inside that callback function.
Recaptcha 有一个onload回调,它会在加载 recaptcha 后运行。将您的代码放在该回调函数中。
https://developers.google.com/recaptcha/docs/display
https://developers.google.com/recaptcha/docs/display
<script>
function onloadCallback() {
/* Place your recaptcha rendering code here */
}
</script>
<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit"></script>
回答by diEcho
I have solved this by ordering the script in below manner
我已经通过以下方式订购脚本解决了这个问题
HTML
HTML
<div id="review_recaptcha"></div>
jQuery
jQuery
<script type="text/javascript">
var review_recaptcha_widget;
var onloadCallback = function() {
if($('#review_recaptcha').length) {
review_recaptcha_widget = grecaptcha.render('review_recaptcha', {
'sitekey' : '<?php echo $site_key?>'
});
}
};
</script>
<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit" async defer></script>
回答by Walk
My Problem got resolved by doing following changes in the script code (:
通过对脚本代码进行以下更改,我的问题得到了解决(:
i.e from internal path
即来自内部路径
<script src='static/js/recaptcha/api.js'></script>
<script src='static/js/recaptcha/api.js'></script>
to external google path i.e
到外部谷歌路径即
<script src='https://www.google.com/recaptcha/api.js'></script>
<script src='https://www.google.com/recaptcha/api.js'></script>
回答by Gabriel Jaime Sierra Rua
Sometimes the application loads several times the script 'https://www.google.com/recaptcha/api.jsafter refresh, make sure your application doesn't have multiple <script async="" defer="" src="https://www.google.com/recaptcha/api.js"></script>
有时应用程序会'https://www.google.com/recaptcha/api.js在刷新后多次加载脚本,请确保您的应用程序没有多个<script async="" defer="" src="https://www.google.com/recaptcha/api.js"></script>
回答by Kamlesh
It works on landing page and also in bootstrap 4 popup form:
它适用于登陆页面,也适用于 bootstrap 4 弹出形式:
<script src="https://www.google.com/recaptcha/api.js?render=ADD-YOUR-RECAPTCHA-SITE-KEY-HERE"></script>
<script>
var interval = setInterval(function(){
if(window.grecaptcha){
grecaptcha.ready(function() {
grecaptcha.execute('ADD-YOUR-RECAPTCHA-SITE-KEY-HERE', {action: 'homepage'}).then(function(token) {
$('#i-recaptcha').prepend('<input type="hidden" name="g-recaptcha-response" value="' + token + '">');
});
});
clearInterval(interval);
}
}, 100);
</script>
Thanks for asking this question. :)
感谢您提出这个问题。:)
回答by mahatmanich
You can set the hl=envariable when calling the api if language is of any concern for you!
如果您hl=en关心语言,您可以在调用 api 时设置变量!
as such:
像这样:
<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit&hl=en"></script>

