What is fetch?
fetch is the JavaScript function built into browsers for making HTTP requests from a page without reloading it.
You call fetch with an address and, optionally, a method, headers and a body. It returns a promise that resolves to a response object, from which you read the status and the body. It is also available in Node and most server runtimes.
For forms, fetch lets you send a submission in the background and show a message on the same page. One thing catches people out: fetch only rejects when the request could not be made at all. A 400 or 500 answer still resolves, so check response.ok before telling the visitor it worked.
A request to another site is subject to CORS, so the receiving service must allow it. FormTango allows requests from any website on its form endpoints.
javascript
const response = await fetch('https://formtango.com/f/YOUR_FORM_ID', {
method: 'POST',
headers: { Accept: 'application/json' },
body: new FormData(form),
});
if (!response.ok) {
// show an error message
}