laravel-recaptchav3 icon indicating copy to clipboard operation
laravel-recaptchav3 copied to clipboard

Error with Ajax

Open josuelmm opened this issue 4 years ago • 4 comments

Every time a request is sent using ajax, and there is an error, just let it do it once, you have to reload the page to be able to use it again, because it returns the catpcha error. It should have a function to refresh the captcha. I made a login where it returns if the person has active, double authentication, so if it returns yes, it should ask for the code on the same page, but it doesn't stop because if it sends the code at once, it should refresh.

josuelmm avatar Jun 23 '20 00:06 josuelmm

@josuelmm you must reset recaptcha when ajax returned success. Below is example code: grecaptcha.execute(captchaID, { action: form.attr('action') }). then(function(token) { form.find("[name='g-recaptcha-response']").val(token); });

I hole I helped :)

Mesiek avatar Dec 23 '21 12:12 Mesiek

@josuelmm you must reset recaptcha when ajax returned success. Below is example code: grecaptcha.execute(captchaID, { action: form.attr('action') }). then(function(token) { form.find("[name='g-recaptcha-response']").val(token); });

I hole I helped :)

Hi . Tanx . but what should be captchaID ? i get this error "Uncaught ReferenceError: captchaID is not defined"

arbabi2010 avatar May 14 '22 12:05 arbabi2010

Every time a request is sent using ajax, and there is an error, just let it do it once, you have to reload the page to be able to use it again, because it returns the catpcha error. It should have a function to refresh the captcha. I made a login where it returns if the person has active, double authentication, so if it returns yes, it should ask for the code on the same page, but it doesn't stop because if it sends the code at once, it should refresh.

i am facing same issue, pls give me solution if anyone has

manishsutariya avatar Aug 06 '23 06:08 manishsutariya

@manishsutariya In my case when I'm creating Bootstrap 5 Modal "in the fly" from AJAX loaded content and in this content I have implemented {!!RecaptchaV3::field('YOUR_RULE_NAME')!!}, reCaptcha hidden input has no value tag, eg. below:

<input type="hidden" name="g-recaptcha-response" id="g-recaptcha-response-XXX">

So I figured out after my modal.show(); I have added:

grecaptcha.execute('YOUR_SITE_KEY', {
    action: 'YOUR_RULE_NAME'
}).then(function(token) {
    var form = document.getElementById('YOUR_FORM_ID');
    form.querySelector("[name='g-recaptcha-response']").value = token;
});

and it works! :) I hope it will help someone!

PS. You can take "SITE_KEY" from <script> tag like eg.

var site_key = document.querySelector('script[src^="https://www.google.com/recaptcha/api.js"]').src.split('=').pop();

rabat1313 avatar Sep 18 '23 11:09 rabat1313