J

JavaScript Handbook

Clean • Professional

AJAX Applications & Examples in JavaScript

2 minute

AJAX Applications & Examples

AJAX (Asynchronous JavaScript and XML) enables web pages to send and receive data from a server asynchronously — without reloading the entire page.

It allows web applications to be faster, interactive, and user-friendly.

How AJAX Works (Quick Recap)

  1. Event Occurs → User triggers an event (like typing, clicking, or selecting).
  2. JavaScript Creates XMLHttpRequest / Fetch → Sends request to server.
  3. Server Processes Request → May interact with database or files.
  4. Server Sends Response → Data is returned (often JSON or text).
  5. Browser Updates Page Dynamically → Only part of the page changes.

Common AJAX Applications

ApplicationDescription
Form ValidationValidate form fields instantly without reloading the page.
Live Search (Autocomplete)Show search suggestions while typing.
Auto Refresh / Live Data UpdateUpdate stock prices, weather info, or notifications in real time.
Chat ApplicationsExchange messages instantly using background AJAX requests.
Data SubmissionSubmit form data to the server and show results immediately.
Content LoadingLoad parts of a webpage dynamically (e.g., comments, images, or pagination).
Voting / Rating SystemSend user votes or ratings asynchronously.
Real-time NotificationsDisplay messages or alerts without reloading the page.

Popular Real-world Examples

1. Live Search Example

<input type="text" id="search" placeholder="Search...">
<div id="result"></div>

<script>
document.getElementById("search").addEventListener("keyup", function() {
  const query = this.value;
  if (query.length > 0) {
    const xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
      if (this.readyState === 4 && this.status === 200) {
        document.getElementById("result").innerHTML = this.responseText;
      }
    };
    xhr.open("GET", "search.php?q=" + encodeURIComponent(query), true);
    xhr.send();
  } else {
    document.getElementById("result").innerHTML = "";
  }
});
</script>

2. Submitting a Form Without Reloading

<form id="myForm">
  <input type="text" name="name" placeholder="Enter name">
  <input type="email" name="email" placeholder="Enter email">
  <button type="submit">Submit</button>
</form>
<p id="message"></p>

<script>
document.getElementById("myForm").addEventListener("submit", function(e) {
  e.preventDefault();
  const formData = new FormData(this);

  fetch("submit.php", { method: "POST", body: formData })
    .then(response => response.text())
    .then(data => document.getElementById("message").innerHTML = data)
    .catch(err => console.error(err));
});
</script>

3. Auto-Refreshing Data (Example: Stock Price)

<div id="stockPrice">Loading...</div>

<script>
function updateStock() {
  fetch("stock.php")
    .then(response => response.text())
    .then(data => document.getElementById("stockPrice").innerHTML = data);
}

setInterval(updateStock, 5000); // Update every 5 seconds
updateStock(); // Initial call
</script>

4. Simple Voting System

<button onclick="vote('up')">👍</button>
<button onclick="vote('down')">👎</button>
<p id="result"></p>

<script>
function vote(type) {
  fetch("vote.php?vote=" + type)
    .then(res => res.text())
    .then(data => document.getElementById("result").innerHTML = data);
}
</script>

Examples in Popular Websites

WebsiteAJAX Usage
Google SearchShows instant results as you type (Live Search).
YouTubeLoads comments, suggestions, and video lists dynamically.
Facebook / TwitterLoads posts, notifications, and messages without refreshing.
GmailUpdates inbox and sends emails asynchronously.
Amazon / FlipkartUpdates product filters, prices, and reviews dynamically.

Advantages of AJAX Applications

  • Faster interaction — no full-page reloads
  • Reduces bandwidth usage
  • Real-time user experience
  • Seamless background data exchange
  • Works with any server-side technology (PHP, ASP, Node.js, etc.)

Limitations

  • No browser history for AJAX-loaded content
  • Requires JavaScript support
  • Complex error handling
  • Security concerns with improper data handling

Article 0 of 0