Chapter 25: AJAX Response

AJAX – Server Response, written as if I’m sitting next to you — explaining everything slowly, clearly, and step by step like a patient teacher who wants you to really understand what happens when the server answers, how to handle different kinds of responses, what can go wrong, and how real developers deal with it in 2025–2026.

We will go very slowly and thoroughly.

1. What actually happens when the server responds?

When you send an AJAX request (whether with XMLHttpRequest, fetch, or any library), the browser does the following behind the scenes:

  1. Sends the HTTP request
  2. Waits for the server to reply
  3. Receives the response which consists of several important parts:
Part of the response What it contains How you access it in code (XHR) How you access it in fetch
Status code 200, 404, 500, 403… xhr.status response.status
Status text “OK”, “Not Found”, “Internal Server Error”… xhr.statusText response.statusText
Response headers Content-Type, Cache-Control, Set-Cookie… xhr.getResponseHeader(“Content-Type”) response.headers.get(“Content-Type”)
Response body The actual data (JSON, XML, text, HTML…) xhr.responseText or xhr.response response.text() / .json() / .blob()…
Response type How the body was interpreted xhr.responseType Determined by which method you call

The most important thing to understand first:

The server response is not automatically usable. You must check if it was successful and parse it correctly.

2. The three most important questions you must always ask

When the server responds, good AJAX code always asks these three questions:

  1. Did the network connection work? → onerror / catch / response.ok
  2. Did the server understand and accept the request? → status between 200–299
  3. What kind of data did the server send back? → Look at Content-Type header and parse accordingly

3. Realistic Example – Handling Server Response (classic XMLHttpRequest style)

HTML

4. What you should carefully observe

  1. Status 200–299 → success path
  2. Status 400–599 → server understood the request but had a problem
  3. Status 0 → network failure (offline, CORS block, aborted, invalid URL…)
  4. Content-Type header tells you how to interpret the body
    • application/json → should use .json() or responseType = “json”
    • text/xml or application/xml → responseXML
    • text/plain, text/html → responseText
  5. responseType changes how the body is delivered

5. Modern way – Using fetch (what you should prefer today)

JavaScript

Quick Summary – Server Response Handling Cheat Sheet

Question you must answer How to check (XHR) How to check (fetch) Typical action
Did network work? onerror or status === 0 catch block Show “No internet” / “Server unreachable”
Is status successful? status >= 200 && status < 300 response.ok Proceed to parse body
What kind of data came back? xhr.getResponseHeader(“Content-Type”) response.headers.get(“content-type”) Choose .json(), .text(), .blob()…
Is it JSON? responseType = “json” or check header response.json() Parse automatically
Is it XML? xhr.responseXML response.text() + DOMParser Use DOM methods to read

Would you like to continue with one of these next?

  • Different Content-Types (JSON, XML, HTML, image, file download…)
  • How to automatically detect the right parsing method
  • Upload progress and download progress
  • Handling very large responses (streaming)
  • CORS, timeout, abort, and retry patterns
  • Converting this knowledge to modern fetch + async/await

Just tell me what you want to explore more deeply! 😊

You may also like...

Leave a Reply

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