Submit Form with AJAX — Working Code for 2026
Submit a form via AJAX (no page reload) using vanilla JS fetch — and the equivalent jQuery $.ajax for legacy codebases. Both POST to splitforms's free /api/submit endpoint.
AJAX form submission means intercepting the form's native submit event with JavaScript, POSTing the form data via fetch (or XMLHttpRequest historically), and updating the DOM with the response — all without a page reload. The user sees an inline success or error message rather than the browser navigating to a new page.
Vanilla JS does this in 15 lines. `addEventListener('submit')` catches the submit; `e.preventDefault()` blocks the native form submission; `new FormData(form)` serializes the form fields; `fetch('/api/submit', { method: 'POST', body: formData })` POSTs them. Await the response, parse the JSON, update the status element. Done.
jQuery's `$.ajax` is the legacy equivalent — same mechanics, different syntax. If your codebase already uses jQuery, the jQuery version is fine. If you're starting fresh, use fetch — no dependency, native browser support since ~2017.
splitforms's `/api/submit` returns JSON: `{ success: true, message: 'Submission received' }` on success, `{ success: false, message: '...', code: '...' }` on error. Your UI logic is just `if (data.success) { ... }`. CORS headers are permissive so cross-origin POSTs from any frontend work without preflight gymnastics.
jQuery $.ajax equivalent (for legacy codebases)
How to set this up
Intercept the submit event
form.addEventListener('submit', handler) — call e.preventDefault() to block native submission.
Serialize fields with FormData
new FormData(form) collects all named fields. No manual serialization required.
POST via fetch
fetch(url, { method: 'POST', body: formData }) — fetch sets the correct Content-Type automatically.
Update UI from the response
await res.json(), check data.success, render inline status message. Form.reset() on success.
15 lines of vanilla JS, no library, inline status, no page reload.
Frequently asked questions
How do I submit a form using AJAX in 2026?
Use fetch with a FormData body. Add a submit listener that calls e.preventDefault, then `fetch(url, { method: 'POST', body: new FormData(form) })`. ~15 lines of vanilla JS, no jQuery needed.
Why not just use jQuery $.ajax?
fetch is native since Chrome 42 / Firefox 39 (2015). It removes a 90KB dependency, has a cleaner Promise-based API, and is the modern standard. Use $.ajax only if your existing codebase already requires jQuery.
Do I need to set a Content-Type header?
No. fetch with a FormData body sets multipart/form-data automatically, including the boundary string. If you set it manually you'll break the boundary.
How do I handle CORS errors when submitting via AJAX?
Check that the backend allows your origin. splitforms's /api/submit sends Access-Control-Allow-Origin: *, so cross-origin POSTs from any frontend work. If you see CORS errors, you're probably using the wrong URL or your backend doesn't allow your origin.
How do I show a progress bar for file uploads?
fetch doesn't support upload progress (yet). Use XMLHttpRequest if you need it: xhr.upload.addEventListener('progress', e => ...). For typical contact forms with no files, fetch is fine.
How do I handle the response after AJAX submit?
splitforms returns JSON: { success, message, submission_id }. Parse with res.json(), check data.success, render an inline message. Don't redirect — that defeats the no-reload UX.
Related guides
Ship the form, not the backend.
Free for 1,000 submissions/month. Email delivery, AI spam filtering, signed webhooks, real dashboard — all on the free plan. No credit card.
Get a free access key →