gpt4 book ai didi

javascript - 不可见的 reCAPTCHA 以多种形式发送空的 g-recaptcha-response

转载 作者:行者123 更新时间:2023-11-29 16:43:46 25 4
gpt4 key购买 nike

我正在尝试使用 Google Invisible reCAPTCHA ,但是当我在同一页面中有多个表单时,它会发送空的 g-recaptcha-response POST 参数。这是我的代码:

谷歌JS

<script src="//google.com/recaptcha/api.js?hl=pt-BR&onload=captchaCallback&render=explicit" async defer></script>

表格 1

<form action="/site/Contact/send" id="form1">
<input type="text" name="nome" required>

<div class="g-recaptcha"
data-sitekey="xxxxxxxxxxxxxxxxxxxxxxxx"
data-callback="form1Callback"
data-size="invisible">
</div>

<button type="submit">Send</button>

</form>

表格 2

<form action="/site/Contact/send" id="form2">
<input type="text" name="nome" required>

<div class="g-recaptcha"
data-sitekey="xxxxxxxxxxxxxxxxxxxxxxxx"
data-callback="form2Callback"
data-size="invisible">
</div>

<button type="submit">Send</button>
</form>

我的 JS(基于 this answer ]

$(document).ready(function() {

window.captchaCallback = function(){
$('.g-recaptcha').each(function(index, el) {
var attributes = {
'sitekey' : $(el).data('sitekey'),
'size' : $(el).data('size'),
'callback' : $(el).data('callback')
};

grecaptcha.render(el, attributes);
});
};

window.form1Callback = function(){
$('#form1').submit();
};

window.form2Callback = function(){
$('#form2').submit();
};
});

当我提交这些表单之一时,g-recaptcha-response 参数发送为空,如下所示。

enter image description here

有人可以帮我把它付诸实践吗?

最佳答案

如果您在 div 元素中呈现不可见的 recaptcha,则需要手动调用 grecaptcha.execute() 来运行 recaptcha。此外,如果有多个表单带有 recaptcha,则需要调用 grecaptcha.execute() 方法,并在调用 grecaptcha.render() 方法时为每个 recaptcha 生成一个小部件 ID。

$(document).ready(function() {
window.captchaCallback = function(){
$('.g-recaptcha').each(function(index, el) {
var attributes = {
'sitekey' : $(el).data('sitekey'),
'size' : $(el).data('size'),
'callback' : $(el).data('callback')
};

$(el).data('recaptcha-widget-id', grecaptcha.render(el, attributes));
});
};

window.form1Callback = function(){
$('#form1').data("recaptcha-verified", true).submit();
};

window.form2Callback = function(){
$('#form2').data("recaptcha-verified", true).submit();
};

$('#form1,#form2').on("submit", function(e){
var $form = $(this);
if ($form.data("recaptcha-verified")) return;

e.preventDefault();
grecaptcha.execute($form.find(".g-recaptcha").data("recaptcha-widget-id"));
});
});

关于javascript - 不可见的 reCAPTCHA 以多种形式发送空的 g-recaptcha-response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43209772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com