Skip to the solution
MediumFrontend6 min read

How do you make an AJAX request?

387 views

The problem

Question presented to candidate: "The term 'AJAX' predates fetch and Promises entirely. What was the original API used to make an AJAX request, and why would you choose fetch over it in new code today?"

What a strong answer should cover:

  • πŸ“Œ Interview term: AJAX (Asynchronous JavaScript And XML) β€” not a specific API itself, but the general TECHNIQUE of fetching/sending data to a server in the background, without a full page reload β€” despite the name, modern AJAX overwhelmingly uses JSON, not XML.
  • πŸ“Œ Interview term: XMLHttpRequest (XHR) β€” the original, classic browser API for making an AJAX request, predating both fetch and Promises β€” it uses an event-based, callback-driven model (onreadystatechange/onload/onerror), tracking progress through a numeric readyState.
  • πŸ“Œ Interview term: fetch() β€” the modern, Promise-based replacement, covered in more depth in this bank's own dedicated fetch API question β€” genuinely built into every current browser and modern Node.js, verified directly.
  • πŸ“Œ Interview term: the real, direct answer to why fetch is generally preferred β€” a cleaner, Promise-based interface (genuinely composable with async/await, chaining, and Promise.all) versus XHR's older, more verbose, callback/event-based model β€” verified directly that fetch genuinely returns a real Promise instance.
  • A precise answer names a real, genuine advantage XHR still has over plain fetch: XHR provides a native upload/download progress event (onprogress), something plain fetch does not expose as simply β€” this is a real, honest reason some file-upload code still reaches for XHR (or a library built on it) today, rather than presenting fetch as strictly superior in every respect.

Clarifying questions expected:

  • None β€” this is a definitional/historical-technical question; naming the real API that predates fetch (XHR) and the concrete reason for preferring fetch today is the strong signal.

Code / implementation expected: Optional β€” confirming fetch is genuinely available and Promise-returning, contrasted with XMLHttpRequest's absence in this Node environment, demonstrates real understanding of the historical/environment context.

  • ajax
  • fetch
  • xmlhttprequest
  • api

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal proof: both fetch and XMLHttpRequest are genuinely available here (a real browser); fetch genuinely returns a real Promise

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 97 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

Why would you choose a WeakSet over a Set to track a group of DOM elements, and what happens to its entries once those elements are removed?

Continue