不可见的reCAPTCHA发送具有多种形式的空g-captcha-chasponse

Mar*_*ato 2 javascript forms jquery recaptcha invisible-recaptcha

我正在尝试使用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>
Run Code Online (Sandbox Code Playgroud)

表格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>
Run Code Online (Sandbox Code Playgroud)

表格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>
Run Code Online (Sandbox Code Playgroud)

我的JS(基于此答案

$(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();
    };
});
Run Code Online (Sandbox Code Playgroud)

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

在此处输入图片说明

有人可以帮我把它投入工作吗?

ono*_*azu 5

如果要在div元素中渲染不可见的Recaptcha,则需要手动调用grecaptcha.execute()来运行recaptcha。另外,如果带有Recaptcha的形式多种多样,则需要在调用grecaptcha.render()方法时使用为每个Recaptcha生成的小部件ID调用grecaptcha.execute()方法。

$(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"));
    });
});
Run Code Online (Sandbox Code Playgroud)


Sou*_*Xin 1

根据文档和您的代码,我可以猜测您正在尝试使用Programmatically invoke the challenge.Google reCaptcha。所以在你的 JS 代码中你错过了一条语句:

grecaptcha.execute();

更新 也许我误解了你的问题,所以检查一下:

渲染显式 onload 可选。是否显式渲染小部件。默认为 onload,这将在它找到的第一个 g-recaptcha 标签中呈现小部件。

据我了解,它只是找到了第一个标记的标签,这会导致您出现问题吗?