hibiscus/public/requests.js

65 lines
1.7 KiB
JavaScript
Raw Normal View History

2024-03-18 15:01:09 +03:00
async function postData(url = "", data = "") {
const response = await fetch(url, {
method: "POST",
credentials: "same-origin",
headers: {
"Content-Type": "text/plain",
},
redirect: "follow",
referrerPolicy: "no-referrer",
body: data,
});
if (response.ok) {
return response.text();
} else {
return response.status
}
}
async function getData(url = "", data = "") {
const response = await fetch(url, {
method: "GET",
credentials: "same-origin",
redirect: "follow",
referrerPolicy: "no-referrer"
});
if (response.ok) {
return response.text();
} else {
console.log(response.text())
return response.status
// return "Error"
}
}
function saveLog() {
let logField = document.getElementById("log")
postData("/api/log", logField.value).then((data) => {
if (data !== 500) {
logField.value = ""
}
});
}
function saveToday() {
let logField = document.getElementById("day")
postData("/api/today", logField.value).then((data) => {
console.log(data);
});
}
function loadToday() {
let dayField = document.getElementById("day")
getData("/api/today", dayField.value).then((data) => {
if (data === 404) {
dayField.value = ""
} else if (data === 401) {
dayField.enabled = false
dayField.value = "Unauthorized"
} else if (data === 500) {
dayField.enabled = false
dayField.value = "Internal server error"
} else {
dayField.value = data
}
});
}