Skip to content

Instantly share code, notes, and snippets.

@justsml
Created November 16, 2018 23:21
Show Gist options
  • Save justsml/ef2e356bec0ef7c6e528d84a5f75ba7e to your computer and use it in GitHub Desktop.
Save justsml/ef2e356bec0ef7c6e528d84a5f75ba7e to your computer and use it in GitHub Desktop.
postForm('http://example.com/api/v1/users', 'form#userEdit')
.then(data => console.log(data))
function postForm(url, formSelector) {
const formData = new FormData(document.querySelector(formSelector))
return fetch(url, {
method: 'POST', // 'GET', 'PUT', 'DELETE', etc.
body: formData // a FormData will automatically set the 'Content-Type'
})
.then(response => response.json())
.catch(error => console.error(error))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment