【问题标题】:Invisible reCAPTCHA sending empty g-recaptcha-response with multiple forms隐形 reCAPTCHA 发送具有多种形式的空 g-recaptcha-response
【发布时间】:2017-08-29 19:17:25
【问题描述】:

我正在尝试使用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 参数发送为空,如下所示。

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

【问题讨论】:

  • 如果某个答案解决了您的问题,请将其标记为您接受的答案。这样,世界其他地方就知道哪种解决方案适用于您的特定情况。
  • @Aram,你是对的,但没有一个答案能解决问题,我现在每页使用单个表单

标签: javascript jquery forms recaptcha invisible-recaptcha


【解决方案1】:

如果您在 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"));
    });
});

【讨论】:

    【解决方案2】:

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

    grecaptcha.execute();

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

    render 显式 onload 可选。是否渲染小部件 明确地。默认为 onload,它将在 它找到的第一个 g-recaptcha 标签。

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

    【讨论】:

    • 默认的onload只在找到的第一个标签中渲染g-recaptcha,因此我改为explicit,这样我可以加载两个元素
    猜你喜欢
    • 1970-01-01
    • 2015-08-21
    • 2015-02-25
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多