jQuery·jQuery from selection to migration

AJAX with jQuery

Request JSON, handle failures and update the page safely.

Beginner 14 min read Lesson 6 of 300 Challenge included
What you will be able to do
  • Use $.getJSON for simple reads.
  • Handle .fail and check response status.
  • Escape data before inserting it.
  • Show loading and empty states.

In plain English

$.ajax and $.getJSON wrap XMLHttpRequest with promises. They still reject on network errors, not on HTTP 404 or 500, so check status. Prefer fetch in new code, but read the old patterns confidently.

ajax.jsJavaScript
$.getJSON('/api/lessons')
  .done((lessons) => {
    $('#list').empty()
    lessons.forEach((lesson) => $('#list').append($('<li>').text(lesson.title)))
  })
  .fail(() => $('#status').text('Could not load lessons'))
Your turn

Load JSON

Easy

Write a $.getJSON call to /api/courses that logs the result.

JAVASCRIPT · starter
$.getJSON('/api/courses')
Check your understanding before moving on.
Knowledge checkQuestion 1

Which callback runs when the request fails?

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: Forms and validation
Lesson 6 of 300 · 0% of course