Created
August 30, 2021 21:27
-
-
Save killa-kyle/33ac139fac25a275983a1801fd06add5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Handle form submission | |
document.querySelector("form").addEventListener("submit", e => { | |
// Cancel default behavior of sending a synchronous POST request | |
e.preventDefault(); | |
// Create a FormData object, passing the form as a parameter | |
const formData = new FormData(e.target); | |
// Send form data to the server with an asynchronous POST request | |
fetch("https://thejsway-server.herokuapp.com/animals", { | |
method: "POST", | |
body: formData | |
}) | |
.then(response => response.text()) | |
.then(result => { | |
document.getElementById("result").textContent = result; | |
}) | |
.catch(err => { | |
console.error(err.message); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment