Chapter 10: XML HttpRequest

1. What is XMLHttpRequest really?

XMLHttpRequest is a built-in browser API that allows JavaScript code running in the browser to make HTTP requests to a server — without reloading the entire page.

It is the technology that made AJAX (Asynchronous JavaScript and XML) possible.

Important truth in 2025–2026:

  • The name contains “XML” because originally it was mainly used to receive XML data
  • Today almost nobody uses it for XML — most use JSON
  • But the name never changed — so we still call it XMLHttpRequest

Think of it like this:

It’s the old, reliable school bus that still runs every day — even though newer, shinier electric buses (fetch + async/await) exist.

Many companies, old codebases, tutorials, and libraries still use XHR, so you must understand it.

2. Why was it invented? (Quick history – just the important part)

Before ~2005:

  • Every time you clicked something → full page reload
  • Very slow and bad user experience

Then Google Maps (2004–2005) showed the world:

“We can update only part of the page by talking to the server in the background!”

XMLHttpRequest became the standard way to do this.

Later → jQuery .ajax(), then fetch(), but XHR is still everywhere in legacy code.

3. Basic Structure – Step by Step

JavaScript

This is the classic pattern — you will see it in millions of codebases.

4. Real Practical Examples

Example 1 – Simple GET request (most common use)

JavaScript

Example 2 – POST request with JSON data

JavaScript

Example 3 – Showing loading state (very common UX pattern)

JavaScript

5. Important Properties & Events You Must Know

Property / Event What it contains / when it fires Typical usage
xhr.readyState 0 = unsent, 1 = opened, 2 = headers received, 3 = loading, 4 = done Check if request is complete (usually wait for 4)
xhr.status HTTP status code (200, 404, 500…) Check success: 200–299
xhr.statusText Status message (“OK”, “Not Found”, …) Show in error messages
xhr.responseText Response as string Most common when expecting JSON / text
xhr.responseXML Parsed XML document (if server sent Content-Type: application/xml) Used when really working with XML
xhr.response Depends on responseType (can be JSON, Blob, ArrayBuffer…) Modern usage
onload Fires when request completes (status 0 or 200–599) Main success/error handler
onerror Fires on network failure (no internet, CORS block…) Handle network-level errors
onprogress Progress events (upload/download) Show progress bars

6. Common Patterns & Modern Additions

JavaScript
JavaScript

7. Very Common Mistakes Beginners Make

  1. Forgetting to call xhr.send()
  2. Sending JSON without Content-Type: application/json
  3. Not parsing responseText → treating string as object
  4. Not handling errors (status !== 200)
  5. Using synchronous mode (xhr.open(…, false)) → freezes browser!
  6. Not checking readyState === 4 when using onreadystatechange
  7. Ignoring CORS errors

Quick Summary – XMLHttpRequest Cheat Sheet

  • new XMLHttpRequest() → create
  • .open(method, url, async) → configure
  • .setRequestHeader() → add headers
  • .send(data?) → start request
  • onload / onerror → handle response
  • responseText / response → get data
  • Most used today: JSON, POST, GET, progress

Even though fetch() is nicer, you will meet XHR in:

  • Old company code
  • WordPress plugins
  • Legacy admin panels
  • Some chart libraries
  • File upload components
  • Code you read in tutorials/books

So understanding XHR is still very useful in 2025–2026.

Would you like to continue with one of these next?

  • Comparison: XMLHttpRequest vs fetch() (detailed side-by-side)
  • Real file upload example with progress bar using XHR
  • How XHR works with CORS and common errors
  • onreadystatechange vs onload – when to use which
  • How to timeout an XHR request
  • Using XHR with XML data (real old-school example)

Tell me which direction feels most useful for you right now! 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *