mediumFrontend

How do you make an AJAX request?

387 views
01

Understand the problem

Fundamental for understanding how web applications communicate with servers.

ajaxfetchxmlhttprequestapi
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Stuck? AI Nudge Available

Get a conceptual hint to guide your logic without spoiling the final implementation.

03

Study the solution

The solution is waiting

Give it an honest attempt first — then compare your thinking with the full walkthrough.

04

Read the code

fetch (modern) vs XMLHttpRequest (legacy)
// Modern AJAX with fetch + async/await
async function loadUser(id) {
  const res = await fetch("/api/users/" + id);
  if (!res.ok) throw new Error("HTTP " + res.status);
  return res.json();
}

// The legacy equivalent with XMLHttpRequest
const xhr = new XMLHttpRequest();
xhr.open("GET", "/api/users/1");
xhr.responseType = "json";
xhr.onload = () => { if (xhr.status === 200) console.log(xhr.response); };
xhr.onerror = () => console.error("network error");
xhr.send();
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.