Skip to content

Instantly share code, notes, and snippets.

@WesleySmits
Created Sep 7, 2022
Embed
What would you like to do?
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