api js
This commit is contained in:
69
server/frontend/main/api.js
Normal file
69
server/frontend/main/api.js
Normal file
@@ -0,0 +1,69 @@
|
||||
function getToken() {
|
||||
return localStorage.getItem("token") || sessionStorage.getItem("token");
|
||||
}
|
||||
function tokenCheck(){
|
||||
const token = getToken();
|
||||
if (!token) {
|
||||
window.location.href = "http://localhost:5500/server/frontend/auth/login.html";
|
||||
}
|
||||
}
|
||||
|
||||
tokenCheck()
|
||||
document.getElementById('logoutForm').addEventListener('submit', async function (e) {
|
||||
e.preventDefault();
|
||||
localStorage.removeItem("token");
|
||||
sessionStorage.removeItem("token");
|
||||
tokenCheck();
|
||||
});
|
||||
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
const form = document.querySelector(".form-info");
|
||||
|
||||
form.addEventListener("submit", async (e) => {
|
||||
e.preventDefault();
|
||||
// try {
|
||||
// собираем данные из формы
|
||||
const name = document.getElementById('ffname').value || "";
|
||||
const middlename = document.getElementById('fmname').value || "";
|
||||
const surname = document.getElementById('flname').value || "";
|
||||
const text_field = document.getElementById('text_field')?.value || "";
|
||||
const food = document.querySelector('input[name="food"]:checked')?.value || "";
|
||||
const types_of_alco = Array.from(document.querySelectorAll('input[name="drink"]:checked'))
|
||||
.map(el => el.value)
|
||||
.join(', ');
|
||||
|
||||
const guestData = {
|
||||
name: name,
|
||||
middlename: middlename,
|
||||
surname: surname,
|
||||
text_field: text_field,
|
||||
activated: true,
|
||||
types_of_food: food,
|
||||
types_of_alco: types_of_alco
|
||||
};
|
||||
console.log(guestData)
|
||||
|
||||
// отправка на /api/update с Authorization
|
||||
// const updateResponse = await fetch('/api/update', {
|
||||
// method: 'POST',
|
||||
// headers: {
|
||||
// 'Content-Type': 'application/json',
|
||||
// 'Authorization': `Bearer ${token}`
|
||||
// },
|
||||
// body: JSON.stringify(guestData)
|
||||
// });
|
||||
|
||||
// if (!updateResponse.ok) {
|
||||
// throw new Error('Ошибка при отправке формы');
|
||||
// }
|
||||
|
||||
// const updateData = await updateResponse.json();
|
||||
// console.log('Форма успешно отправлена:', updateData);
|
||||
|
||||
// } catch (err) {
|
||||
// console.error(err);
|
||||
// alert('Ошибка: ' + err.message);
|
||||
// }
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user