Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 39 ยท Web APIs and AJAX Basics

What Is AJAX?

Learn what AJAX is, how background requests differ from full page loads, and how XMLHttpRequest evolved into fetch(). Includes live examples.

Open a weather site and change the city. The temperature updates, but the page doesn't blink, the scroll position stays put and the menu keeps its state. Nothing was reloaded. The page quietly asked a server for new data, got an answer and changed only the part that needed changing. That technique is called AJAX.

AJAX stands for Asynchronous JavaScript and XML. The name is a historical accident: modern code rarely uses XML, and the technique is not a technology of its own. It is a pattern: JavaScript sends an HTTP request in the background, the browser keeps running, and when the reply arrives your code updates the page. This tutorial teaches that pattern with fetch() and JSON, the tools almost everyone uses today.

Two ways to talk to a server

A traditional web page works by navigation. You click a link or submit a form, the browser throws away the current page, asks the server for a new one and renders it from scratch. Every interaction costs a full round trip, and everything on screen is replaced, even the parts that didn't change.

With AJAX, JavaScript makes the request itself. The current page stays alive, and the response is data (usually JSON) rather than a whole new document.

sequenceDiagram
    participant U as User
    participant B as Browser page
    participant S as Server
    U->>B: Clicks "Next city"
    B->>S: fetch("/api/weather?city=Oslo")
    Note over B: Page stays interactive
    S-->>B: JSON: { "temp": 4 }
    B->>B: JavaScript updates one element
    B-->>U: New temperature shown

A background request: the page keeps running while the server answers, then JavaScript updates one element.

The word asynchronous is the key. If the browser waited (blocked) until the server replied, the whole page would freeze for as long as the network took. Instead, your code starts the request, the browser goes on handling clicks and scrolling, and a callback or promise tells your code when the answer is ready. If asynchronous code is new to you, read Asynchronous JavaScript and Promises first.

What changes on the wire

Both approaches use the same protocol, HTTP, and the same servers. The difference is what you ask for and who does the asking.

Full page load AJAX request
Started by The browser (link, form, address bar) Your JavaScript
Response is A complete HTML document Usually JSON, sometimes HTML fragments or text
Current page Replaced Stays alive
Page state (scroll, typed text, focus) Lost Kept
Who decides what updates The browser re-renders everything Your code updates exactly what it chooses

AJAX is not always better. A page that can be a simple link, such as a blog post, should usually stay a link: links work without JavaScript, can be bookmarked and are easy for search engines to read. Use AJAX where staying on the page is part of the experience: live search, saving a draft, loading more results, updating a cart.

A short history

The idea is older than the name.

  • Late 1990s: Microsoft added an XMLHttpRequest object to Internet Explorer for Outlook Web Access. Other browsers copied it, and by the mid-2000s it was available everywhere.
  • 2005: a magazine article gave the technique the name AJAX, and maps and webmail apps showed what it made possible.
  • 2006 onward: libraries such as jQuery hid the awkward parts of XMLHttpRequest behind friendly functions like $.ajax().
  • 2015 and later: fetch() arrived with promises built in, followed by async and await. Today it is the standard in browsers, Node.js, Deno and Bun.

You will still meet XMLHttpRequest in older code, and a later lesson shows how to read it and convert it. For new code, use fetch().

The request and response cycle

Every AJAX call, whatever the tool, follows the same four steps.

  1. Your code builds a request: a method (such as GET or POST), a URL, optional headers and an optional body.
  2. The browser sends it and keeps running. You get a promise (or an event later) instead of a result.
  3. The server processes it and sends back a response: a status code, headers and a body.
  4. Your code reads the response and updates the page, or handles an error.

The browser is the client, the program that asks. The server is the program that answers. The client never reaches into the server's database; it can only ask through the server's public interface, which is why servers publish APIs (covered in a later lesson). The server never pushes into your page on its own either, unless you set up a special channel such as a WebSocket (also covered later). It speaks only when spoken to.

flowchart LR
    A["1. Build request<br/>method, URL, headers, body"] --> B["2. Send<br/>browser keeps running"]
    B --> C["3. Server replies<br/>status, headers, body"]
    C --> D["4. Read response<br/>update the page"]

The four steps of every AJAX call.

Your first background request

The examples in this tutorial run in a sandbox that cannot reach real servers, so the live demos use mock data. A data: URL holds a file's content inside the URL itself, so fetch() can read it as if it were a tiny server. Your code uses the same fetch() calls as it would with https://api.example.com/....

JS fetch() does not block your code

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice the order of the messages. Line 2 and line 3 are printed before the reply, because fetch() does not wait; only await does. That gap is what keeps pages responsive.

Updating a page without reloading it

Here is the classic AJAX moment in a real page. Press the button several times: only the paragraph changes. The text you type in the box and the counter stay exactly as they are, which a full page load would have reset.

Update one part of the page

The three lines at the end of the handler do the job that a whole page load used to do: they take data and put it where it belongs.

What runs where. In this tutorial's live editors you can run fetch() against data: and blob: URLs and against mock functions that return real Response objects. Calls to real servers such as https://api.example.com/... are shown in read-only code blocks, because the sandbox has no network access to them. The code is the same in both cases.

What the server sends back

AJAX responses come in a few common shapes:

  • JSON: structured data ({"city":"Oslo","temp":4}). This is the default choice, and the next chapters teach it properly.
  • HTML fragments: ready-made markup you insert into the page. Libraries such as htmx lean on this style.
  • Plain text, CSV or other formats: for logs, exports and simple values.
  • Binary data: images, audio and files, read as a Blob.

Whatever the shape, the request and the cycle are the same. You pick a reader on the response (json(), text(), blob()) that matches the body.

Common beginner mix-ups

  • "AJAX is a library." It is a pattern. fetch(), XMLHttpRequest and jQuery's $.ajax() are all ways to do it.
  • "The data is ready on the next line." It isn't. The reply arrives later, so the code that uses it must run after await or inside .then().
  • "AJAX can call any website." The browser enforces the same-origin policy: a page may read replies only from its own origin unless the other server opts in with CORS. Headers, Auth and CORS Basics explains it.
  • "Faster is guaranteed." Background requests save work, but each one still takes network time. Good AJAX interfaces show loading and error states.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What makes an AJAX request different from clicking a normal link?

    AJAX requests are started by your JavaScript over normal HTTP, and the page keeps running while the answer comes back. A link replaces the whole page.
  2. 2. What does the A (asynchronous) in AJAX buy you?

    Asynchronous means your code does not block waiting for the reply, so scrolling, typing and clicking keep working. It does not make the network faster or let the server initiate contact.
  3. 3. Which tool is the modern standard for making AJAX requests?

    fetch() returns promises and works with async and await. XMLHttpRequest still exists in older code, and JSONP is a historical workaround that should be avoided.
  4. 4. In the cycle of an AJAX call, which step comes immediately after the server sends its reply?

    The client always acts last: your code reads the status and body and decides what to show. The server cannot touch your page.

Summary

  • AJAX is a pattern: JavaScript makes an HTTP request in the background and updates the page with the reply.
  • The page is not replaced, so scroll position, typed text and focus are kept.
  • XMLHttpRequest started it; fetch() with promises and async/await is the modern way.
  • Every call has four steps: build the request, send it, the server replies, your code reads the reply.
  • The client asks and the server answers; the server cannot reach into your page on its own.
  • In this tutorial's sandbox, live demos use data: and blob: URLs and mock functions; real servers appear in read-only code.

Next: HTTP for AJAX Developers โ€” methods, status codes and headers, the vocabulary every request speaks.