Files

68 lines
2.3 KiB
JavaScript
Raw Permalink Normal View History

2026-01-19 08:35:00 +03:00
// Общие функции для AJAX запросов
async function sendRequest(url, data) {
try {
const response = await fetch(url, {
method: 'POST',
2026-01-19 08:57:58 +03:00
credentials: 'same-origin',
2026-01-19 08:35:00 +03:00
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data)
});
2026-01-19 08:57:58 +03:00
if (!response.ok) {
return { success: false, message: 'Ошибка сервера' };
}
2026-01-19 08:35:00 +03:00
return await response.json();
} catch (error) {
console.error('Ошибка:', error);
return { success: false, message: 'Ошибка сети' };
}
}
// Обработка регистрации
if (document.getElementById('registerForm')) {
document.getElementById('registerForm').addEventListener('submit', async function(e) {
e.preventDefault();
const formData = new FormData(this);
const data = Object.fromEntries(formData);
const result = await sendRequest('api/register.php', data);
if (result.success) {
alert(result.message);
window.location.href = 'login.html';
} else {
alert('Ошибка: ' + result.message);
}
});
}
// Обработка входа
if (document.getElementById('loginForm')) {
document.getElementById('loginForm').addEventListener('submit', async function(e) {
e.preventDefault();
const formData = new FormData(this);
const data = Object.fromEntries(formData);
2026-01-19 15:27:18 +03:00
if (!data.site_alias) {
const metaAlias = document.querySelector('meta[name="site-alias"]');
if (metaAlias && metaAlias.content) {
2026-01-19 16:41:30 +03:00
data.site_alias = metaAlias.content.trim();
2026-01-19 15:27:18 +03:00
} else {
const path = window.location.pathname.replace(/\/+$/, '');
const parts = path.split('/').filter(Boolean);
2026-01-19 16:41:30 +03:00
data.site_alias = parts[0] || 'root';
2026-01-19 15:27:18 +03:00
}
}
2026-01-19 08:35:00 +03:00
const result = await sendRequest('api/login.php', data);
if (result.success) {
alert(result.message);
window.location.href = 'index.html';
} else {
alert('Ошибка: ' + result.message);
}
});
}