What is AJAX?

Lesson 1 of 12 3 min read Free lesson

How pages request data in the background, what requests and responses are, and setting up a local server to practise.

Pages that update themselves

Without AJAX, every time you send a form or click a link, the browser throws the page away and loads a new one. With AJAX, JavaScript quietly asks the server for data and changes only the part of the page that needs changing.

You already use it every day: search suggestions, "like" buttons, chat messages and infinite scrolling all work this way.

Without AJAXWith AJAX
The whole page reloadsOnly part of the page changes
The screen flashes whiteSmooth, app-like feel
Scroll position and typed text are lostEverything else stays as it is
The server sends a whole HTML pageThe server usually sends small JSON data

Request and response

  1. JavaScript sends a request to a URL, for example search.php?q=html.
  2. The server (PHP) does its work, such as a database query.
  3. The server sends back a response, usually JSON data.
  4. JavaScript reads the response and updates the page.

Set up your practice folder

AJAX needs a real web server, because browsers block requests from pages opened straight from your disk.

  1. Install XAMPP and start Apache (and MySQL for the later lessons).
  2. Create the folder htdocs/learn-ajax.
  3. Open your pages at http://localhost/learn-ajax/..., never by double-clicking.

Check your understanding

Quick check

What does AJAX let a page do?

Quick check

What format do most AJAX responses use today?

Quick check

Why open AJAX pages through http://localhost/...?

Hi there! đź‘‹ Have a question about the courses or joining the class? Ask me.