jQuery AJAX Introduction
AJAX stands for Asynchronous JavaScript and XML. It lets a web page communicate with a server and update parts of the page without reloading the entire page. jQuery makes AJAX extremely simple compared to writing raw JavaScript XMLHttpRequest code.
What Problem Does AJAX Solve?
In traditional websites, every action that requires data from the server — searching, loading comments, submitting a form — causes the whole page to reload. The user sees a blank screen, waits, and the page appears again. AJAX removes that full-page reload completely.
Real-Life Analogy
Think of a restaurant. In the old model, every time you order one item, the waiter takes your entire table back to the kitchen and returns later with a brand new table. With AJAX, the waiter only takes your order slip, goes to the kitchen, and brings back just the dish you ordered — while you sit at the same table the whole time.
Diagram: Page Without vs With AJAX
WITHOUT AJAX:
User action → Full request sent to server
→ Server sends full HTML page
→ Browser reloads everything
→ User sees blank screen during load
WITH AJAX:
User action → Small request sent to server (just data)
→ Server sends only the needed data
→ jQuery updates ONLY the changed part
→ User sees instant, smooth update ✓
What AJAX Can Do
- Load data from a server after the page has loaded.
- Send form data to a server without refreshing.
- Fetch JSON data and build dynamic content.
- Load HTML fragments and inject them into the page.
- Poll the server at regular intervals for updates (live feeds, notifications).
How AJAX Works — Step by Step
Step 1: User triggers an action (button click, form submit, scroll)
│
Step 2: jQuery sends an HTTP request to the server in the background
│
Step 3: Server processes the request and sends back a response
(HTML, JSON, XML, plain text)
│
Step 4: jQuery receives the response
│
Step 5: jQuery updates the specific part of the page with the new data
jQuery AJAX Methods Overview
| Method | Purpose |
|---|---|
| $.ajax() | Full-featured AJAX request with all options |
| $.get() | Quick GET request to fetch data |
| $.post() | Quick POST request to send data |
| $.getJSON() | Fetch JSON data via GET |
| $.getScript() | Load and execute a JavaScript file |
| .load() | Load HTML into an element from a URL |
A Simple AJAX Example with load()
HTML:
<div id="content"></div>
<button id="loadBtn">Load News</button>
jQuery:
$(document).ready(function() {
$("#loadBtn").click(function() {
$("#content").load("news.html");
});
});
Clicking the button loads the content of news.html and places it inside the #content div — no page refresh.
Synchronous vs Asynchronous
The "A" in AJAX stands for Asynchronous. This means the browser does not wait for the server to respond before running the next line of code. The rest of the page continues working while the request is in progress.
Synchronous (blocking):
Code runs → WAIT for server → Code continues
User sees frozen page during the wait.
Asynchronous (non-blocking):
Code runs → Request sent → Code continues immediately
Server responds later → Callback runs
User sees a fully responsive page throughout.
Data Formats AJAX Handles
- JSON — most common today; JavaScript objects as text.
- HTML — ready-to-inject fragments.
- XML — structured data (older, less common now).
- Plain text — simple string responses.
The Same-Origin Policy
Browsers allow AJAX requests only to the same domain by default. If your page is on example.com, it cannot fetch data from otherdomain.com unless that server allows it through a mechanism called CORS (Cross-Origin Resource Sharing). You will learn more about this in the AJAX error handling topic.
Quick Summary
- AJAX sends and receives data from a server without reloading the full page.
- It makes websites faster and more interactive.
- jQuery provides simple methods like
$.ajax(),$.get(),$.post(), and.load(). - AJAX is asynchronous — the page stays responsive while waiting for a server response.
- The server can respond with JSON, HTML, XML, or plain text.
