fixed fetch

This commit is contained in:
ishak jmilou.ishak
2024-11-04 11:34:42 +01:00
parent 21ed9c5080
commit 41034ba85c

View File

@@ -1,14 +1,25 @@
window.onload = function () { document.getElementById("form").addEventListener("click", function(event) {
const form = document.getElementById("form"); event.preventDefault(); // Prevent the form from reloading the page
form.onclick = function (event) {
event.preventDefault(); // Check if the target of the click event is a button
fetch("/move", { if (event.target.tagName === "BUTTON") {
method: "GET", const direction = event.target.value; // Get the direction from the button's value
// Send the direction data to the server using fetch
fetch("/", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ direction: direction })
}) })
.then((response) => {}) .then(response => response.json())
.then(() => { .then(data => {
fetch console.log("Success:", data);
// Optional: Update UI based on server response if needed
})
.catch(error => {
console.error("Error:", error);
});
}
}); });
};
};
9