started with fetch

This commit is contained in:
ishak jmilou.ishak
2024-10-24 13:23:37 +02:00
parent c84c0d2cef
commit 80771dc740
2 changed files with 14 additions and 20 deletions

View File

@@ -1,19 +1,13 @@
document.querySelectorAll('button').forEach(button => { window.onload = function () {
button.addEventListener('click', function() { const form = document.getElementById("form");
const direction = this.value; form.onclick = function (event) {
fetch('/move', { event.preventDefault();
method: 'POST', fetch("/move", {
headers: { method: "GET",
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({ direction: direction }),
}) })
.then(response => response.json()) .then((response) => {})
.then(data => { .then(() => {
document.getElementById('response').innerText = data.message; fetch
})
.catch(error => {
document.getElementById('response').innerText = 'Er is een fout opgetreden: ' + error;
});
});
}); });
};
};

View File

@@ -12,7 +12,7 @@
<img src="kobuki.jpg" alt="Kobuki Robot" id="robot-image" /> <img src="kobuki.jpg" alt="Kobuki Robot" id="robot-image" />
</div> </div>
<div class="button-section"> <div class="button-section">
<form action="/" method="post"> <form id = "form" action="/" method="post">
<button class="btn" name="direction" value="left"></button> <button class="btn" name="direction" value="left"></button>
<button class="btn" name="direction" value="up"></button> <button class="btn" name="direction" value="up"></button>
<button class="btn" name="direction" value="right"></button> <button class="btn" name="direction" value="right"></button>