AJAX & Async Requests·AJAX from first request to live updates

Forms without a reload

Submit forms with fetch and render field errors.

Beginner 13 min read Lesson 5 of 300 Challenge included
What you will be able to do
  • Use event.preventDefault().
  • Use FormData for multipart submissions.
  • Render field errors next to inputs.
  • Re-enable the submit button in finally.

In plain English

Intercept submit, build FormData, send it and render the server response. Keep native validation as a convenience, but always send the data to the server for real validation.

form.jsJavaScript
form.addEventListener('submit', async (event) => {
  event.preventDefault()
  const button = form.querySelector('button')
  button.disabled = true
  try {
    const response = await fetch('/api/signup', { method: 'POST', body: new FormData(form) })
    if (!response.ok) throw new Error('Save failed')
  } finally {
    button.disabled = false
  }
})
Your turn

Prevent the reload

Easy

Stop the default submit event.

JAVASCRIPT · starter
form.addEventListener('submit', (event) => {
Check your understanding before moving on.
Knowledge checkQuestion 1

Which API builds a multipart body from a form?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: Error handling
Lesson 5 of 300 · 0% of course