Listen for the form's submit event, call preventDefault, send new FormData(form) with fetch, then write a message into the page based on the response. That is the whole technique.
The details that matter are the ones people skip: checking the response, handling a network failure, and keeping the form usable when JavaScript does not load.
The markup
Keep a real action and method on the form. If the script fails to load, the form still works as an ordinary post.
<form id="contact" action="https://formtango.com/f/YOUR_FORM_ID" method="POST">
<label>Email <input type="email" name="email" required></label>
<label>Message <textarea name="message" required></textarea></label>
<button type="submit">Send</button>
<p id="contact-status" role="status"></p>
</form>
The role="status" on the message element means screen readers announce the text when it changes.
The script
Put this in a script tag after the form, one statement per line:
const form = document.getElementById('contact');
const status = document.getElementById('contact-status');
form.addEventListener('submit', async (event) => {
event.preventDefault();
const button = form.querySelector('button');
button.disabled = true;
try {
const response = await fetch(form.action, { method: 'POST', headers: { Accept: 'application/json' }, body: new FormData(form) });
const result = await response.json();
if (response.ok && result.ok) { form.reset(); status.textContent = 'Thanks! Your message has been sent.'; }
else { status.textContent = result.error || 'Something went wrong. Please try again.'; }
} catch (error) { status.textContent = 'Could not reach the server. Check your connection and try again.'; }
button.disabled = false;
});
Why each part is there
- preventDefault stops the browser's own form post, which would leave the page.
- new FormData(form) collects every named field, including files. Do not set a Content-Type header: the browser sets the right one.
- The Accept: application/json header asks for a JSON answer in place of a redirect.
- fetch only throws on a network failure. A 400 or 500 answer does not throw, so check response.ok yourself.
- Disabling the button stops a double click from sending the message twice.
- The browser still runs the required and type checks before the submit event fires, so built-in validation keeps working.
How FormTango answers
A request that asks for JSON gets { ok: true, id } when the submission is stored, or { ok: false, error } with a message you can show. JSON bodies are accepted too, if you would rather send JSON than FormData. Requests from any website are allowed on the form endpoint, so there is no CORS setup.
If you want this without writing the script, add the FormTango drop-in script and mark the form with a data-formtango attribute. It sends in the background and shows the message for you. See the JavaScript guide.
Common mistakes
- Building the body by hand and missing a field. Pass the form element to FormData.
- Setting Content-Type to multipart/form-data yourself, which drops the boundary the server needs.
- Showing "sent" without checking the response.
- Using mode: 'no-cors'. The response becomes unreadable, so you can never tell whether it worked.
Common questions
- How do I submit a form without reloading the page?
- Handle the submit event, call preventDefault, and send new FormData(form) with fetch. Then show a message in the page from the response. There is a copyable version in the [submit without redirecting guide](/guides/submit-form-without-redirect).
- Should I send FormData or JSON with fetch?
- FormData is simpler: it reads every named field from the form and supports file uploads. Send JSON only if the receiver needs it, and then set Content-Type to application/json.
- Does fetch throw an error on a 404 or 500 response?
- No. fetch rejects only when the request could not be made at all. For error statuses you must check response.ok or response.status.
Written by the FormTango team. Facts about other services were read from their own pages on November 22, 2026. Spotted something out of date? Tell us and we will fix it.