简体   繁体   中英

reCAPTCHA v3 - Error: No reCAPTCHA clients exist

First of all, I am aware that the same problem was already discovered here: Error: No reCAPTCHA clients exist (reCAPTCHA v3) But since the answers there, didn't lead me to a solution, I try my luck here.


So I tried using reCAPTCHA, since I get alot of spam mails from the form on my webpage. In my HTML head I have that code:

<script src="https://www.google.com/recaptcha/api.js?render=MY_SITE_KEY"></script>
<script>
grecaptcha.ready(function() {
  grecaptcha.execute("MY_SITE_KEY").then(function(token) {
    console.log(token);
  });
});
</script>

to load the Captcha and which generates a token. When I submit my form, I call the following ajax code:

$.ajax({
  type: 'POST',
  url: $(form).attr('action'),
  data: {
    name: name,
    email: email,
    message: message,
    captcha: grecaptcha.getResponse()
}).done(function(response){ ... })

and finally in PHP I do the following:

<?php

  $secret = "MY_SECRET_KEY";
  $response = $_POST["captcha"];
  $verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
  $captcha_success=json_decode($verify);

    if ($captcha_success->success==false) {
      echo "reCaptcha indentified you as a bot. We don't like bots here.";
    }
    else if ($captcha_success->success==true) {

      // MY WHOLE mail() function here

    }

?>

When I submit the form then, I get the error:

Uncaught Error: No reCAPTCHA clients exist.
    at Gw (recaptcha__de.js:511)
    at Object.Q5 [as getResponse] (recaptcha__de.js:519)
    at HTMLFormElement.<anonymous> (main.js:265)
    at HTMLFormElement.dispatch (jquery.min.js:3)
    at HTMLFormElement.q.handle (jquery.min.js:3)

What have I done wrong? I followed Googles instructions, but maybe I missed something.

You can re-implement in the following method:

<script src="https://www.google.com/recaptcha/api.js?render=MY_SITE_KEY"></script>
<script>
var grecaptchaSiteKey = 'MY_SITE_KEY';

var _RECAPTCHA = _RECAPTCHA || {};

_RECAPTCHA.init = function() {
    grecaptcha.ready(function() {
        grecaptcha.execute(grecaptchaSiteKey, {action: 'homepage'}).then(function(token) {
            if (jQuery(form)[0]) {
                if (jQuery('.grecaptchaToken')[0]) {
                    jQuery(form).find('.grecaptchaToken').remove();
                }

                jQuery(form).append('<input type="hidden" class="grecaptchaToken" name="grecaptchaToken" value="' + token + '" />');
            }
        });
    });
}

_RECAPTCHA.init();

</script>

After that you can get the value of the hidden input and put that in the Ajax Payload.

Now, to avoid errors during a second form submission, you can call the _RECAPTCHA.init() method; within the successful callback of the Ajax call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM