22

I've integrated reCAPTCHA v3 in one of my forms. In onload, there's a token produced and google captcha logo in the bottom right corner. But when I submit the form, in console there is an error shown, "Error: No reCAPTCHA clients exist". Also, it seems, no data is fetched by "g-recaptcha-response" and $_POST["g-recaptcha-response"] remains empty.

Here is the sample code:

<script type="text/javascript">
    var ReCaptchaCallbackV3 = function() {
        grecaptcha.ready(function() {
            grecaptcha.execute("site_key").then(function(token) {
                console.log("v3 Token: " + token);
            });
        });
    };
</script>

<script src="https://www.google.com/recaptcha/api.js?onload=ReCaptchaCallbackV3&render=site_key"></script>

It doesn't produce any "g-recaptcha-response" when the form is submitted.

I don't know much about google reCaptcha. I've followed the documentation provided by them and used a site and a secret key in the proper way.

Can anybody please tell me where might be the problem and what is the possible solution?

Hossain Amin
  • 223
  • 1
  • 2
  • 5

8 Answers8

22

I believe this error occurs when the reCaptcha api.js loads, but your container is not present on the page yet (at least for v2). I had this error occur in a React app when I navigated to the page rather than loading it as the first on. Instead of using render=explicit and using a global namespace onLoadCallback, I was able to resolve it by rendering the captcha element manually.

Instead of creating a <div class="g-recaptcha"></div>, give the container div an id only (<div id="recaptcha-container"></div>) and render it in your JS code (e.g. in componentDidMount for a React app):

grecaptcha.ready(function() {
  grecaptcha.render("recaptcha-container", {
    "sitekey": "your-site-key"
  });
});
Max
  • 1,313
  • 14
  • 11
  • To give some more color to this, you also don't even have to specify an id; you can pass a reference to the HTML element (e.g. an angular `ElementRef`'s `nativeElement`). – ollien Jan 04 '23 at 18:41
10

When I came across this problem with reCAPTCHA v3, it was because I didn't pass it the correct site key.

Piquan
  • 452
  • 4
  • 8
  • Yes, for me i was passing undefined by mistake. The error should be more descriptive in this case, but unfortunately it is not. – ehab Jan 25 '21 at 11:51
  • Same for us @ehab, we were passing undefined by mistake. The error should be more descriptive in this case, but unfortunately it is not. – Chris Apr 26 '23 at 21:56
7

Have you tried loading the script before trying to send the request?

<script src="https://www.google.com/recaptcha/api.js?onload=ReCaptchaCallbackV3&render=site_key"></script>
<script type="text/javascript">
    var ReCaptchaCallbackV3 = function() {
        grecaptcha.ready(function() {
            grecaptcha.execute("site_key").then(function(token) {
                console.log("v3 Token: " + token);
            });
        });
    };
</script>
2

This also happens in Recaptcha 2 before user interacts with it. So, I have a submit button that triggers a JS function that checks the value of recaptcha. To solve the no client exists problem, I did:

try {
   data["reCaptcha"] = grecaptcha.getResponse();
} catch (err) {
   data["reCaptcha"] = "";
}

The data object then gets sent to a back-end script that validates the recaptcha. The back-end also checks for the field being empty.

Lenka Pitonakova
  • 979
  • 12
  • 14
2

I had this problem because I was calling grecaptcha.reset(); when there wasn't any Recaptcha active on the site

grecaptcha.reset();
recaptcha__en.js:507 Uncaught Error: No reCAPTCHA clients exist.
    at MX (recaptcha__en.js:507)
    at Object.Zn [as reset] (recaptcha__en.js:514)
    at <anonymous>:1:13
SSpoke
  • 5,656
  • 10
  • 72
  • 124
1

I was using React and only rendering my captcha container sometimes. Fixed by hiding the captcha button instead of not rendering it.

Stuart Rucker
  • 189
  • 3
  • 7
1

In my case grecaptcha.reset(); was present from previous code snippet, and I was programmatically invoking it.

As the captcha verification is going on the fly every time. Resetting wasn't required.
After eliminating this grecaptcha.reset(); my code works perfectly fine.

Maybe this hint can help someone.

Piyush Pranjal
  • 414
  • 4
  • 11
0

We received this because we have a second environment with another domain and forgot to add this domain to the reCaptcha admin site. The solution was to add the new domain to the settings. The steps are Open www.google/recaptcha/admin/site Select the site you're working on Click on the settings gear Add the domain in its respective section