How to integrate reCAPTCHA to the ajax queries od DataTables
How to integrate reCAPTCHA to the ajax queries od DataTables
            
                            
                                  in DataTables             
        Hello!
We decides to protect all our APIs with google reCAPTCHA and we do not have problems with other APIs. But I cannot understand how to integrate reCAPTCHA token request before ajax query of DataTables and how to send reCAPTCHA token within ajax query.
Please help me! Your help will be much appreciated!
This question has accepted answers - jump to:
Answers
Assuming you are using the
ajaxoption you can use it as an object to pass the token. This is from the docs:Other options are to use
ajax.dataorpreXhrto modify the Ajax request.If this doesn't help then please provide more details of how you want to pass the token.
Kevin
Thank you for your prompt response!
It looks like that ajax.data or preXhr are synchronous in nature, but recaptcha token can be requested only like this and I cannot understand how to integrate this in ajax.data
function getReCAPTCHAToken() {
return new Promise((resolve) => {
grecaptcha.enterprise.ready(function() {
grecaptcha.enterprise.execute('key', {action: 'action'}).then(function(token) {
resolve(token);
});
});
});
}
In that case I think you will need to use
ajaxas a function so you can fetch the recaptcha token first then place the ajax request to fetch the data using jQuery ajax() or your favorite method.Kevin
Found this old example that may be useful.
https://live.datatables.net/nokeduka/1/edit
Kevin
Oh, thank you so much! It finally worked! The way I did it
Looks good, thanks for the update.
Kevin