I
I
Igor2018-11-12 13:53:36
JavaScript
Igor, 2018-11-12 13:53:36

How to initialize reCAPTCHA V3 via js?

The fact is that if you do the initialization in the standard way, then after successfully submitting the form via AJAX, the error Uncaught Error: No reCAPTCHA clients exist appears in the browser console. and it is not possible to resubmit the form until the page is reloaded.

I just need to re-render the captcha, but this requires its id.

grecaptcha.render(
    container,
    parameters
)


If I initialize the captcha following the invisible (v2) example, then the error Invalid site key or not loaded in api.js appears .

<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit" async defer></script>
<input type="hidden" id="g-recaptcha-response" name="g-recaptcha-response">
<div id="rcv3_ajaxform"></div>
<script>
    var onloadCallback = function() {
        var = widget_ajaxform = grecaptcha.render('rcv3_ajaxform', {
            'sitekey' : '00000000000-publick-key-000000000000'
        });
        grecaptcha.execute('widget_ajaxform', { action: 'ajaxform' })
        .then(function(token) {
            document.getElementById('g-recaptcha-response').value=token;
        });
    };
</script>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mmman, 2018-12-11
@mmman

Every time you need something from Recaptcha v3 (initialization, reload) call it in the format

grecaptcha.execute('ключ_сайта', {action: 'если_надо'})
    .then(function(token) {
        ....
    });
});

She will figure out what to do now.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question