我正在尝试使用Google Invisible reCAPTCHA,但是当我在同一页面中有多个表单时,它会发送 g-recaptcha-response
POST参数 . 这是我的代码:
Google JS
<script src="//google.com/recaptcha/api.js?hl=pt-BR&onload=captchaCallback&render=explicit" async defer></script>
Form 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>
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>
My 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
参数将被发送为空,如下所示 .
有人可以帮我把它投入使用吗?
2 回答
如果要在div元素中呈现不可见的重新接收,则需要手动调用grecaptcha.execute()来运行recaptcha . 此外,如果有多个带recaptcha的表单,则需要调用grecaptcha.execute()方法,并在调用grecaptcha.render()方法时为每个recaptcha生成一个小部件ID .
根据文档和你的代码,我猜你正试图从Google reCaptcha使用
Programmatically invoke the challenge.
. 所以在你的JS代码中你错过了一个语句:grecaptcha.execute();
UPDATE 也许我误解了你的问题,所以检查一下:
据我所知,它刚刚发现第一个标记的标签,这会导致你的问题?