Загрузка данных


// ===============================
// CAPTCHA ДЛЯ АВТОРИЗАЦИИ
// ===============================

// Получаем новый пример с сервера.
async function loadLoginCaptcha() {
    const question = document.getElementById(
        "loginCaptchaQuestion"
    );

    const answer = document.getElementById(
        "loginCaptchaAnswer"
    );

    if (!question) {
        return;
    }

    question.textContent = "Загрузка примера...";

    if (answer) {
        answer.value = "";
    }

    try {
        const response = await fetch("auth.php", {
            method: "POST",
            headers: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            body: new URLSearchParams({
                action: "captcha"
            })
        });

        const data = await response.json();

        if (!response.ok || !data.success) {
            throw new Error("Не удалось загрузить CAPTCHA");
        }

        question.textContent = data.question;
    } catch (error) {
        question.textContent = "Ошибка загрузки";
        console.error(error);
    }
}

// Кнопка обновления примера.
document.addEventListener("DOMContentLoaded", function () {
    const refreshButton = document.getElementById(
        "refreshLoginCaptcha"
    );

    if (refreshButton) {
        refreshButton.addEventListener(
            "click",
            loadLoginCaptcha
        );
    }

    // Загружаем пример при открытии страницы.
    loadLoginCaptcha();
});