【问题标题】:Google recaptcha v3 recaptcha_response is nullGoogle recaptcha v3 recaptcha_response 为空
【发布时间】:2020-12-22 22:10:40
【问题描述】:

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

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

<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>

我在我的表单中添加了这个:

&lt;input type="hidden" name="recaptcha_response" id="recaptchaResponse"&gt;

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

错在哪里? 感谢您的回答!

【问题讨论】:

    标签: recaptcha


    【解决方案1】:

    我尝试了很多更改,但每次我得到“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) {});
        });
    }

    【讨论】:

      猜你喜欢
      • 2022-01-19
      • 2023-02-07
      • 2019-09-22
      • 2019-01-01
      • 2018-10-28
      • 2019-08-10
      • 2018-12-12
      • 2019-11-08
      • 1970-01-01
      相关资源
      最近更新 更多