How to implement AJAX using JavaScript? – AJAX Examples

As we know Ajax (Asynchronous JavaScript and XML) is a technology to implement partial loading. The idea is in place of sending request to load whole the page from server we can fetch the line of changes we required in our page. This process helps to reduces network load. The result we have better performance & faster web.

Understanding AJAX: The Foundation of Asynchronous Web Requests

What is AJAX?

AJAX allows web pages to send and receive data in the background without reloading. Think of it as a way to fetch data without stopping everything else. Unlike old websites that load entire pages, AJAX updates parts of a page on the fly. It uses JavaScript to send requests and jQuery or vanilla JavaScript to process responses. XML was once popular for data transfer, but JSON is now king because it’s easier to use and lighter.

How AJAX Works in Web Development?

The process begins when a user does something—like clicking a button or scrolling. JavaScript then creates a request to the server. This request runs in the background, so the user doesn’t see the page refresh. When the server responds, JavaScript updates only the affected parts of the website. Here’s a simple visual flow:

User interacts with the page.
JavaScript sends an AJAX request.
Server processes and responds.
JavaScript updates the web page without reloading.

Modern browsers support AJAX smoothly, making it reliable for web apps, mobile sites, and more.

Benefits of Using AJAX

Using AJAX offers many advantages:

Real-time updates keep users engaged.
Less server load since only data, not entire pages, are transferred.
Faster, more responsive websites improve user satisfaction.
Reduced bandwidth consumption means less data usage for users.

In short, AJAX makes websites feel more like apps — fast, interactive, and user-friendly.

Today we have many ready-made libraries are available to make an Ajax Call easier & programmer friendly. But as a core engineer it is required to know what is the basics of an Ajax Call. That’s why I am presenting this topic here. It is also very useful for fresher.

In the below example I am implementing JavaScript AJAX Example, no third-party libraries I used here. Ajax-Demo.html is the file where from I am doing an Ajax Call to Demo-Ajax.txt file to fetch its text contents. To do so on button click I am calling a function demoAjaxCall(). In side this function I created an instance xmlhttpObj for the XMLHttpRequest object. Then using open method I am instructing xmlhttpObj to locate the demo-ajax.txt file. Here in xmlhttpObj.open I am using get method. Finally using send method I am sending request to the server.

Later in xmlhttpObj.onreadystatechange() I am checking readyState & status. If readyState is equal to 4 & status is equal to 200, it mean my Ajax Call is successful. To display the response from Ajax Call here I used xmlhttpObj.responseText to the inner html of the div demoAjax.

JavaScript-AJAX-Example.html

<!DOCTYPE html>
<html>
<head>
<title>JavaScript AJAX Example</title>
<script type="text/javascript">
function demoAjaxCall()
{
var xmlhttpObj;
if (window.XMLHttpRequest)
{
/* Compatiable for IE7+, Mozilla Firefox, Google Chrome, Opera &amp; Safari */
xmlhttpObj=new XMLHttpRequest();
}

xmlhttpObj.onreadystatechange=function()
{
if (xmlhttpObj.readyState==4 &amp;&amp; xmlhttpObj.status==200)
{
document.getElementById("demoAjax").innerHTML='<h2>' + xmlhttpObj.responseText + '</h2>';
}
}
xmlhttpObj.open("GET","demo-ajax.txt",true);
xmlhttpObj.send();
}
</script>
</head>
<body>

<div id="demoAjax"><h2>On below button click this label will show you text from demo-ajax.txt using Ajax Call</h2></div>
<button type="button" onclick="demoAjaxCall()">Trigger an Ajax Call</button>

</body>
</html>

Demo-Ajax.txt

This is the text from demo-ajax.txt file. Your Ajax Call is Successful.

Conclusion

AJAX is a game changer for modern web development. It makes websites faster, more responsive, and more interactive. Whether you’re building simple features like live search or complex real-time systems, understanding AJAX is essential. Start experimenting with the Fetch API and practice integrating AJAX into your projects. As web technology advances, combining AJAX techniques with WebSockets and serverless solutions will unlock even bigger possibilities for dynamic websites. Keep pushing your skills and create websites users love to use.