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

View File

@@ -12,7 +12,7 @@
<img src="kobuki.jpg" alt="Kobuki Robot" id="robot-image" />
</div>
<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="up"></button>
<button class="btn" name="direction" value="right"></button>