Getting Null in g-recaptcha-response Google's reCaptcha

前端 未结 2 1595
南笙
南笙 2021-01-25 22:21

I am trying to implement Google\'s reCaptcha v.2.0 but i am getting null in g-recaptcha-response due to this reCaptcha is not working properly and I am always g

2条回答
  •  梦毁少年i
    2021-01-25 23:13

    I've run into the same issue. The strangest part is a client-side call to grecaptcha.getResponse() returns to correct response. For some reason it's just not setting g-recaptcha-response. So my workaround was to set a data-callback function to populate a hidden field with the response and use that server-side instead. The hidden input also helps with ease of client-side validation.
    eg:

    javascript:

    function captcha_onclick() {
        document.getElementById('recaptchaValidator').value = grecaptcha.getResponse();
    }
    

    server-side:

    if(!empty($_POST['recaptcha'])) {
        $captcha = $_POST['recaptcha'];
        ...
    }
    

提交回复
热议问题