Google recaptcha v3 recaptcha_response为空

问题描述

我正在我的网站上使用google recaptcha v3,

这是我在html网页中添加代码

set @prefixes = 'ml_,nc_,msr_'; SELECT `id`,`name` FROM `submenus` WHERE `status` = 1 AND FIND_IN_SET(`submenus`.`group_prefix`,@prefixes); +----+---------------------------+ | id | name | +----+---------------------------+ | 4 | Mission Lessons Module | | 5 | MSR Module | | 8 | Work Authorization Module | +----+---------------------------+

<script src="https://www.google.com/recaptcha/api.js?render=my public key"></script>

我以我的形式添加了它:

<script> grecaptcha.ready(function () { grecaptcha.execute('my public key',{ action: 'contact' }).then(function (token) { var recaptchaResponse = document.getElementById('recaptchaResponse'); ecaptchaResponse.value = token; }); }); </script>

但是当我提交表单并尝试使用var_dump()查看$ _POST ['recaptcha_response']的内容时 它只是一个空字符串:string(0)“”

错误在哪里? 谢谢答案!

解决方法

我尝试了很多更改,但每次我得到“ token = null ”,然后我就试试这个.. 100% 工作....

在你的 HTML 中

<html>
<head>
</head>
<body>
  <form id="contact">
    <div class="col-12">
      <div class="form-group">
          <input type="hidden" name="captcha_token" id="recaptchaResponse" data-sitekey="YOUR-SITE-KEY">
      </div>
    </div>
    <div class="col-12">
        <button type="submit" class="btn btn-lg" id="submit-btn">
            SUBMIT
        </button>
    </div>
  </form>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
  <script src="https://www.google.com/recaptcha/api.js?onload=ReCaptchaCallbackV3&render=YOUR-SITE-KEY"></script>
</body>
</html>

然后加入你的js文件

$(document).ready(function(){
  setInterval(function(){
        ReCaptchaCallbackV3();    
    },90000);
    
    $(document).on("submit",'#contact',function (e) {
      var token = $('[name="g-recaptcha-response"]').val();
      console.log(token);
    });
});

var ReCaptchaCallbackV3 = function() {
    grecaptcha.ready(function () {
        grecaptcha.execute('YOUR-SITE-KEY',{ action: 'contact' }).then(function (token) {});
    });
}