【问题标题】:Invisible reCaptcha AJAX Call隐形 reCaptcha AJAX 调用
【发布时间】:2018-08-15 23:09:28
【问题描述】:

我正在尝试在网站上实施不可见的 reCaptcha。但我无法让它工作。这是我正在做的事情:

标题

  <!-- Invisible reCaptcha -->
  <script src="https://www.google.com/recaptcha/api.js" async defer></script>

form.php

<form id="contact-form" class="contact-form" action="#contact">
   <p class="contact-name">
     <input id="contact_name" type="text" placeholder="Full Name" value="" name="name" />
   </p>
   <p class="contact-email">
     <input id="contact_email" type="text" placeholder="Your E-Mail Address" value="" name="email" />
    </p>
     <p class="contact-message">
       <textarea id="contact_message" placeholder="Your Message" name="message" rows="15" cols="40"></textarea>
     </p>
     <p class="contact-submit">
       <a type="submit" id="contact-submit" class="submit" href="#">Send Your Email</a>
      </p>
       <div id="recaptcha" class="g-recaptcha"  data-sitekey="6LceN0sUAAAAAPvMoZ1v-94ePuXt8nZH7TxWrI0E" data-size="invisible" data-callback="onSubmit"></div>
       <div id="response">
       </div>
</form>

script.js

// contact form handling
  $(function() {
    $("#contact-submit").on('click',function() {
        $contact_form = $('#contact-form');

        var fields = $contact_form.serialize();
      var test = grecaptcha.execute();
      console.log(fields);
      console.log(test);

        $.ajax({
            type: "POST",
            url: "assets/php/contact.php",
            data: fields,
            dataType: 'json',
            success: function(response) {
                if(response.status){
                    $('#contact-form input').val('');
                    $('#contact-form textarea').val('');
                }

                $('#response').empty().html(response.html);
            }
        });
        return false;
    });
  });

contact.php

private function validateFields(){
        // Check reCaptcha
    if(!$this->captcha){
        echo "Please fill out the reCaptcha correctly";
     }

   $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretkey."&response=".$this->captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
             if(intval($responseKeys["success"]) !== 1) {
                 echo "You are a bot! GO AWAY!";
            }

如果 g-recaptcha-response 不为空,则后端 (contact.php) 工作正常。但是我的问题是当我尝试这样做时,g-recaptcha-response(在 var 字段和测试中)总是空的。当我在表格上显示 recapcha 并填写时,g-recapcha-response 不是空的,一切正常。 我知道我必须在某个地方调用 grecaptcha.execute() ,但即使我这样做了,变量也是空的。如何以编程方式调用 this?

感谢您的每一次帮助!提前谢谢!

【问题讨论】:

  • 你错过了data-callback 吗?我相信这个功能就是获得你发送的价值(作为第一个参数)。由于您通过 ajax 发布表单,因此您可能必须将 ajax 部分移动到回调函数中,这样它就不会过早触发(因为回调没有承诺)。
  • 你好。我忘了复制它。对不起。我正在关注本指南:developers.google.com/recaptcha/docs/… 2. 示例
  • 好的,你在某处有onSubmit = function(token) {} 设置吗?
  • 没有。这就是我所缺少的,我不知道把它放在哪里:(
  • 好的,让我写一个例子,它应该*能在你目前所拥有的基础上继续工作。

标签: javascript php html ajax recaptcha


【解决方案1】:

您缺少onSubmit() 回调函数。

要重新排列您的 js 以利用该功能,这将是您的新 js 块:

<script>
// this block must be defined before api.js is included
function onSubmit(token) {
    var fields = $('#contact-form').serializeArray();             // get your form data
        fields.push({name: "g-recaptcha-response", value: token});// add token to post
    $.ajax({
        type: "POST",
        url: "assets/php/contact.php",
        data: fields,
        dataType: 'json',
        success: function(response) {
            if(response.status) {
                $('#contact-form input').val('');
                $('#contact-form textarea').val('');
            }
            $('#response').empty().html(response.html);
        }
    });
    grecaptcha.reset();// to reset their widget for any other tries
}
</script>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<script>
// this block can be defined anywhere
$(function() {
    $("#contact-submit").on('click',function() {

        // call grecaptcha.execute, which causes recaptcha to
        // do its thing and then calls onSubmit with the token 

        grecaptcha.execute();// does not return anything directly

        return false;
    });
});
</script>

【讨论】:

  • 我现在遇到 onload 不是函数的错误。我在第一个函数前面加了 $( 和 );在末尾。仍然 onload 不是一个函数。有什么想法吗?
  • 经过良好的格式化后,我可以摆脱错误。但是现在我有了这个:ReCAPTCHA 找不到用户提供的功能:onSubmit
  • 我意识到你在 .js 文件中有这些......但不幸的是顺序确实很重要。 onSubmit 函数需要在调用 google js 之前存在于页面中(因为它在加载时执行它的操作)。它会查找已定义的函数。
  • 我在recaptcha js之前的header中添加了第一个用于测试的脚本函数。现在奇怪的事情正在发生。有时当我按下按钮时,什么也没有发生。有时它会打开“选择图像”对话框(耶进步!:))但他不让我点击验证?
  • 有时它会弹出(认为你是一个经常点击它的机器人)。有时不是。有时它只是传递而无需检查,因为它假设您是人类。如果它不允许您单击他们的小部件,我不确定如何解决(可能是 z-index 问题?)。但这会朝着不同的方向/问题发展;)
猜你喜欢
  • 2017-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-25
  • 2017-08-29
  • 1970-01-01
  • 2012-06-29
相关资源
最近更新 更多