async function SubmitData(data) {
let captchaToken;
// Проверка включена ли grecaptcha в настройках
if ($('#grecaptcha').length > 0) {
captchaToken = await getCaptchaToken({action: '123'});
}
console.log(captchaToken);
const response = await postForm(address, formId);
console.log(response);
}
function postForm(sendTo, formId) {
return new Promise(res => {
$.ajax({
type: "POST",
url: sendTo,
data: $(formId).serialize(),
contentType: "application/x-www-form-urlencoded",
dataType: "text",
success: (data) => {
return res(data);
}
});
})
}
function getCaptchaToken(opts) {
const SITE_KEY = 'SITEKEY';
return new Promise((res) => {
$.getScript('https://www.google.com/recaptcha/api.js?render=' + SITE_KEY, () => {
grecaptcha
.ready(() => {
grecaptcha
.execute(SITE_KEY, opts)
.then((token) => {
return res(token);
});
});
});
})
}