JavaScript

JavaScript is a high-level, interpreted programming language that is widely used for web development. Initially designed as a client-side scripting language, it runs directly in web browsers, enabling dynamic and interactive user experiences. JavaScript can now be used for server-side development as well.

AJAX Introduction in JavaScript

15 April 2025 | Category:

What is AJAX?

AJAX stands for Asynchronous JavaScript and XML.

It’s a technique that allows webpages to send and receive data from a server without refreshing the page.

🔥 In simple words: AJAX = Load data without reloading the page!


âś… Why Use AJAX?

With AJAX, you can:

  • Get data from a server without reloading the page
  • Send data to a server behind the scenes
  • Make fast, dynamic, user-friendly web apps (like Gmail, Twitter, Facebook feeds, etc.)

🔄 Traditional Page vs AJAX Page

FeatureTraditional RequestAJAX Request
Page ReloadYes❌ No
User ExperienceSlowerSmooth, fast
Server CommunicationFull page reloadOnly fetches data

⚙️ How AJAX Works (Step-by-Step)

  1. JavaScript sends a request to the server using an AJAX method.
  2. Server processes the request and sends back a response (usually in JSON or XML).
  3. JavaScript receives the response and updates the webpage dynamically.

đź§° Ways to Use AJAX

JavaScript offers multiple ways to use AJAX:

MethodDescription
XMLHttpRequestTraditional AJAX method
fetch() APIModern, simpler AJAX method
AxiosExternal library for AJAX
jQuery AJAXOlder, library-based method

🚀 Example Using fetch() (Modern Way)

<button onclick="loadData()">Load User</button>
<p id="output"></p>

<script>
  function loadData() {
    fetch("https://jsonplaceholder.typicode.com/users/1")
      .then(response => response.json())
      .then(data => {
        document.getElementById("output").innerText = `Name: ${data.name}`;
      })
      .catch(error => console.error("Error:", error));
  }
</script>

✔️ Output: Displays user name fetched from API without page reload.


đź§  Note on JSON vs XML

  • Modern APIs use JSON (JavaScript Object Notation).
  • XML is rarely used now for web APIs.
  • AJAX still keeps the old name but mostly uses JSON today.

âś… Summary

FeatureDescription
AJAXLoad/send data without refreshing the page
Key BenefitSmooth user experience
Toolsfetch(), XMLHttpRequest, Axios
Data TypeJSON (mostly), XML (rare)