Skip to content

Instantly share code, notes, and snippets.

@WesleySmits
Created September 7, 2022 20:25
Show Gist options
  • Save WesleySmits/0877cdf65a3699bb825977c34c64828b to your computer and use it in GitHub Desktop.
Save WesleySmits/0877cdf65a3699bb825977c34c64828b to your computer and use it in GitHub Desktop.
FormData: Fetch
const newsletterForm = document.getElementById('newsletter-form');
newsletterForm.addEventListener('submit', () => {
const formData = new FormData(newsletterForm);
fetch('/some/api/endpoint', {
method: 'post',
body: formData
})
.then((response) => response.json())
.then((result) => {
console.log('Success:', result);
})
.catch((error) => {
console.error('Error:', error);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment