Fundamentals of AJAX Technology
Introduction
AJAX (Asynchronous JavaScript and XML) is a technique used to create dynamic and interactive web pages. It allows web pages to communicate with the server in the background without reloading the entire page.
AJAX is not a programming language; it is a combination of existing web technologies.
Fundamental Concepts of AJAX
Technologies Used in AJAX
AJAX works using a combination of:
Working of AJAX (Basic Flow)
Difference Between Synchronous and Asynchronous Web Application
Web applications communicate with the server either synchronously or asynchronously. In synchronous mode, the browser waits for the server response. In asynchronous mode (AJAX), the browser continues working while the request is processed.
A synchronous request blocks the client until the server completes the operation and returns the response.
- Browser becomes unresponsive while waiting
- Full page refresh occurs
- Tasks execute sequentially
- Slower user experience
An asynchronous request does not block the client. The browser remains responsive and updates only the required part of the page.
- Browser remains responsive
- No full page reload
- Multiple requests can run independently
- Faster and smoother user experience
Comparison Table
| Basis | Synchronous | Asynchronous |
|---|---|---|
| Blocking | Browser is blocked | Non-blocking |
| Page Reload | Full page refresh | Partial update only |
| User Experience | Slow and interruptive | Fast and smooth |
| Execution Flow | Sequential | Independent / parallel |
| Modern Usage | Rarely used | Standard in modern web apps |
XMLHttpRequest Object — Properties and Methods
Definition
The XMLHttpRequest (XHR) object is a built-in browser API used in AJAX to send and receive data from a web server in the background without reloading the entire page. It enables asynchronous communication and dynamic page updates.
Purpose of XMLHttpRequest
- Exchange data with server asynchronously.
- Update part of the web page using DOM.
- Improve responsiveness of web applications.
Creating an XMLHttpRequest Object
var xhr = new XMLHttpRequest();
After creation, open() initializes the request and send() sends it to the server.
Important Properties of XMLHttpRequest
| Property | Description |
|---|---|
| readyState | Indicates the state of the request (values: 0 to 4). |
| status | Returns HTTP status code (e.g., 200 = OK). |
| statusText | Returns status message (e.g., "OK"). |
| responseText | Returns response data as plain text. |
| responseXML | Returns response data as an XML document. |
Important Methods of XMLHttpRequest
| Method | Description |
|---|---|
| open(method, URL, async) | Initializes the request. Specify GET/POST, the URL, and whether async. |
| send() | Sends the request to the server. |
| onload / onreadystatechange | Callback function executed when response is received. |
| setRequestHeader() | Sets an HTTP request header. |
| abort() | Cancels the current request. |
Basic Example
var xhr = new XMLHttpRequest();
xhr.onload = function() {
document.getElementById("demo").innerHTML = xhr.responseText;
};
xhr.open("GET", "data.txt", true);
xhr.send();
Working of AJAX and its Architecture
Working of AJAX
AJAX works by exchanging data between the browser and server in the background without reloading the entire web page. It uses the XMLHttpRequest object for communication.
Steps in AJAX Working
AJAX Architecture
AJAX architecture is the combination of technologies that work together to enable asynchronous web communication.