Y
Y
Y_Koval2020-09-06 17:33:09
JavaScript
Y_Koval, 2020-09-06 17:33:09

Validating request via repcaptcha v3?

I'm not good with JS, so please help me to attach google recaptcha v3 validation to my code. The code itself:

$('.load-more-end-butt').on('click', function(){
                        const btn = $(this);
                        $.ajax({
                            url: './load_more.php',
                            type: 'POST',
                            beforeSend: function(){
                                btn.attr('disabled', true);
                            },
                            success: function(responce){
                                btn.attr('disabled', false);
                                if(responce == 'Ошибка валидации!')
                                    alert('Ошибка. Не удалось подтвердить запрос. Перезагрузите страницу.');
                                else
                                    $('.row-posts-end').before(responce);
                                    AOS.init(),$(document).ready(function(){$('[data-toggle="tooltip"]').tooltip()});
                            },
                            error: function(){
                                alert('Ошибка. Не удалось получить ответ от сервера. Попробуйте перезагрузить страницу.');
                                btn.attr('disabled', false);
                            }
                        });
                    });

And what I tried to do:
grecaptcha.ready(function () {
                       grecaptcha.execute('6Le6zscZAAAAAEbnZH5qAIDh0atgvQcRbQQjw2vk', { action: 'contact' }).then(function (token) {
                            var recaptchaResponse = document.getElementById('recaptchaResponse');
                            recaptchaResponse.value = token;
                            $('.load-more-end-butt').on('click', function(){
                                const btn = $(this);
                                $.ajax({
                                    url: './load_more.php',
                                    type: 'POST',
                                    beforeSend: function(){
                                        btn.attr('disabled', true);
                                    },
                                    success: function(responce){
                                        btn.attr('disabled', false);
                                        if(responce == 'Ошибка валидации!')
                                            alert('Ошибка. Не удалось подтвердить запрос. Перезагрузите страницу.');
                                        else
                                            $('.row-posts-end').before(responce);
                                            AOS.init(),$(document).ready(function(){$('[data-toggle="tooltip"]').tooltip()});
                                    },
                                    error: function(){
                                        alert('Ошибка. Не удалось получить ответ от сервера. Попробуйте перезагрузить страницу.');
                                        btn.attr('disabled', false);
                                    }
                                });
                            });
                        });
                    });

What I want is that when the user clicks a button (thus initializing the code I've already implemented myself), a request is made to Recpatcha v3, which would evaluate the user and give them a token. I know how to validate a token, but don't know how to create one and send it via JS. Therefore, I ask you to help me.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question