Modern web applications are expected to be fast, responsive, and interactive. Users no longer want pages to reload every time they submit a form or request data from a server. This is where AJAX becomes important.
AJAX, which stands for Asynchronous JavaScript and XML, allows web applications to exchange data with a server in the background without refreshing the entire page. This technology helps developers create smoother and more user-friendly applications.
In this blog, we’ll explain what AJAX is, how it works, the role of the XMLHttpRequest object, and how AJAX improves web application performance.
What is AJAX?
AJAX stands for Asynchronous JavaScript and XML. It is not a programming language but a technique used to build dynamic and interactive web applications.
AJAX became popular through applications like Google Suggest, where search suggestions appear instantly while the user types.
With AJAX, JavaScript communicates directly with the server using HTTP requests. This allows web pages to update small portions of content without reloading the complete page.
Technologies Used in AJAX
AJAX combines several web technologies, including:
- JavaScript
- HTML/XHTML
- CSS
- XML
- HTTP Requests
These technologies work together to improve the responsiveness of web applications.
Benefits of AJAX
AJAX offers several advantages for modern web development:
- Faster page updates
- Reduced server load
- Better user experience
- Background data processing
- No full-page reloads
- Improved responsiveness
Because only specific data is transferred instead of an entire webpage, applications become more efficient and interactive.
How AJAX Works
AJAX works through the JavaScript XMLHttpRequest object. This object allows the browser to communicate with the server asynchronously.
The process works as follows:
- A user performs an action on the webpage
- JavaScript creates an HTTP request
- The request is sent to the server
- The server processes the request
- The response is returned to the browser
- JavaScript updates the webpage dynamically
All of this happens without refreshing the page.
AJAX Browser Support
Most modern browsers support AJAX through the XMLHttpRequest object.
Older versions of Internet Explorer used ActiveXObject, while modern browsers use the built-in XMLHttpRequest.
Example: Creating an XMLHttpRequest Object
<script type="text/javascript">
function ajaxFunction()
{
var xmlHttp;
try
{
xmlHttp = new XMLHttpRequest();
}
catch (e)
{
try
{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
}
</script> Understanding AJAX Properties
1. onreadystatechange
The onreadystatechange property stores the function that processes the server response.
xmlHttp.onreadystatechange=function()
{
// Process server response here
} 2. readyState
The readyState property represents the current status of the request.
| State | Description |
|---|---|
| 0 | Request not initialized |
| 1 | Request setup completed |
| 2 | Request sent |
| 3 | Request processing |
| 4 | Request completed |
Example:
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
// Response is ready
}
} 3. responseText
The responseText property retrieves data returned by the server.
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.myForm.time.value = xmlHttp.responseText;
}
} Sending Requests with AJAX
AJAX uses the open() and send() methods to communicate with the server.
Example
xmlHttp.open("GET","time.asp",true);
xmlHttp.send(null); Parameters used:
GET– HTTP request methodtime.asp– Server-side scripttrue– Asynchronous request
Simple AJAX Example
Below is a basic AJAX form example:
<html>
<body>
<form name="myForm">
Name:
<input type="text" onkeydown="ajaxFunction();" name="username" />
Time:
<input type="text" name="time" />
</form>
</body>
</html> When the user types in the text field, AJAX sends a request to the server in the background and updates the time field dynamically.
Server-Side Script Example
The server-side script returns the current server time.
ASP Example
<%
response.write(time)
%> The returned value is stored in responseText and displayed on the webpage.
Why AJAX Matters
AJAX changed the way web applications work by making them faster and more interactive. Instead of waiting for complete page reloads, users can interact with applications smoothly and receive instant updates.
Today, AJAX concepts are widely used in:
- Search suggestions
- Live chat applications
- Online forms
- Social media feeds
- Real-time dashboards
- E-commerce platforms
Conclusion
AJAX is a powerful technique that enables asynchronous communication between browsers and servers. By using JavaScript and the XMLHttpRequest object, developers can build responsive and user-friendly web applications without constantly reloading pages.
Understanding AJAX is essential for modern web development because it forms the foundation of many interactive web technologies used today.
If you require help, contact SupportPRO Server Admin

