Skip to content

Instantly share code, notes, and snippets.

@killa-kyle
Created August 30, 2021 21:27
Show Gist options
  • Save killa-kyle/33ac139fac25a275983a1801fd06add5 to your computer and use it in GitHub Desktop.
Save killa-kyle/33ac139fac25a275983a1801fd06add5 to your computer and use it in GitHub Desktop.
// 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