From 41034ba85cac09fca67935740f705cd724c48b69 Mon Sep 17 00:00:00 2001 From: "ishak jmilou.ishak" Date: Mon, 4 Nov 2024 11:34:42 +0100 Subject: [PATCH] fixed fetch --- src/Python/flask/web/static/script.js | 37 +++++++++++++++++---------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/src/Python/flask/web/static/script.js b/src/Python/flask/web/static/script.js index 44a0489..9c0c481 100644 --- a/src/Python/flask/web/static/script.js +++ b/src/Python/flask/web/static/script.js @@ -1,14 +1,25 @@ -window.onload = function () { - const form = document.getElementById("form"); - form.onclick = function (event) { - event.preventDefault(); - fetch("/move", { - method: "GET", +document.getElementById("form").addEventListener("click", function(event) { + event.preventDefault(); // Prevent the form from reloading the page + + // Check if the target of the click event is a button + if (event.target.tagName === "BUTTON") { + 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(() => { - fetch - }); - }; -}; -9 \ No newline at end of file + .then(response => response.json()) + .then(data => { + console.log("Success:", data); + // Optional: Update UI based on server response if needed + }) + .catch(error => { + console.error("Error:", error); + }); + } +});