Chapter 23: AJAX XMLHttp

1. What is the XMLHttpRequest Object?

XMLHttpRequest (usually abbreviated XHR) is a built-in browser API — it is not a library or framework — it is part of the browser itself.

It allows JavaScript code running in the browser to:

  • Make HTTP requests to a server
  • Do this in the background (asynchronously)
  • Receive the response
  • Update the current web page without reloading the entire page

This is the core technology that made AJAX possible.

Important historical note (you should know this):

  • Created by Microsoft for Internet Explorer 5 in 1999 (originally called XMLHTTP)
  • Became standard across all browsers around 2005–2006
  • The name contains “XML” because originally it was mostly used to receive XML data
  • Today almost nobody uses it for XML — most use JSON, but the name never changed

So when people say “XMLHttpRequest”, they usually just mean “the object that lets JavaScript talk to the server in the background”.

2. Why was XMLHttpRequest revolutionary?

Before XHR:

  • Every interaction with the server (clicking a link, submitting a form, changing a dropdown…) → full page reload
  • Slow, especially on dial-up / early broadband
  • Lost scroll position, form data, etc.

With XMLHttpRequest:

  • You can send small requests (e.g. “check if username is available”, “get new messages”, “load more products”)
  • Get back only the needed data
  • Update only part of the page → Feels fast, modern, smooth

This is what made Gmail, Google Maps, Facebook news feed, Twitter (early version), online spreadsheets, etc. feel magical around 2005–2010.

3. The Life Cycle of an XMLHttpRequest (very important)

Every XHR request goes through these readyState stages:

readyState value Constant name Meaning
0 UNSENT Object created, but open() not called yet
1 OPENED open() was called, but send() not yet
2 HEADERS_RECEIVED send() called, headers and status received
3 LOADING Downloading response body (partial data available)
4 DONE Request finished (success or failure)

Most people only care about readyState 4 + HTTP status 200–299.

4. Basic Structure – Step by Step (classic style)

JavaScript

5. Realistic Example 1 – GET request (classic style)

HTML

6. Key Properties & Events of XMLHttpRequest

Property / Event What it contains / when it fires Typical usage
xhr.readyState 0–4 (see table above) Usually wait for === 4
xhr.status HTTP status code (200, 404, 500…) Success = 200–299
xhr.statusText “OK”, “Not Found”, etc. Show in error messages
xhr.responseText Response as plain string Most common when expecting JSON/text
xhr.responseXML Parsed XML document (if server sent text/xml) Used when really dealing with XML
xhr.response Depends on responseType (text, json, blob…) Modern usage
xhr.onload Fires when request is complete (readyState 4) Main success handler
xhr.onerror Fires on network failure (no internet, CORS…) Handle connection problems
xhr.onprogress Progress events during download Show progress bars

7. Quick Summary – XMLHttpRequest Cheat Sheet

Step Code example
Create new XMLHttpRequest()
Configure xhr.open(“GET”, url, true)
Set headers xhr.setRequestHeader(“Accept”, “application/json”)
Handle response xhr.onload = function() { … }
Handle error xhr.onerror = function() { … }
Send xhr.send() or xhr.send(JSON.stringify(data))
Get data xhr.responseText or xhr.response

8. Modern Advice (2025–2026)

  • Use fetch + async/await for new code — much cleaner
  • Know XMLHttpRequest because:
    • You will see it in old tutorials
    • You will find it in legacy code
    • Some very specific features (upload progress with xhr.upload, older CORS tricks) are easier with XHR
    • Many libraries (some chart libraries, file upload components) still use it internally

Would you like to continue with one of these next?

  • Full POST request example with form data
  • Upload progress bar using XMLHttpRequest
  • How to parse XML response when the server still returns XML
  • Comparison: XMLHttpRequest vs fetch vs axios
  • Handling CORS errors (very common issue)
  • Making AJAX look like old-school (loading spinner, error messages)

Just tell me what you want to practice or understand more deeply! 😊

You may also like...

Leave a Reply

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