\n```\n\n### Classic version with XMLHttpRequest\n```js\nconst xhr = new XMLHttpRequest();\nxhr.open('GET', 'data.php?id=1', true); // true = asynchronous\nxhr.onreadystatechange = function () {\n if (xhr.readyState === 4 && xhr.status === 200) {\n document.getElementById('result').innerHTML = xhr.responseText;\n }\n};\nxhr.send();\n```\n\n## How the page is updated with the response\n1. An event (click, keypress, timer) triggers a JavaScript function.\n2. JavaScript sends an **asynchronous request** to the server (`fetch`/`XMLHttpRequest`).\n3. The server processes it and returns data (JSON, XML, or HTML fragment).\n4. A callback (or `await`) receives the response once it arrives; the page stays usable meanwhile.\n5. JavaScript **parses** the data (`res.json()`) and modifies the **DOM** (`textContent`, `innerHTML`, `appendChild`) to show it.\n\n**Note:** `innerHTML` with untrusted data risks XSS, so prefer `textContent` or sanitize the data."}}}Back to the 2024 paper

Module III: Basics of Web Programming

20247m

What is AJAX (Asynchronous JavaScript and XML), and how does it enhance user experience in web applications? Provide a brief code example of an AJAX request and explain how to update a web page with the response.

Worked SolutionAI Assisted

What is AJAX?

AJAX (Asynchronous JavaScript and XML) is a technique for exchanging data with the server in the background and updating part of a web page without reloading the whole page. Despite the name, it commonly uses JSON today instead of XML.

Components: HTML/CSS (presentation), DOM (dynamic updates), XMLHttpRequest or fetch() (asynchronous requests), JavaScript (logic), and JSON/XML (data format).

How it improves user experience

  1. No full page reloads: only the changed part is refreshed, so the app feels fast and smooth.
  2. Responsiveness: the user keeps working while data loads.
  3. Less bandwidth: only the needed data travels, not the entire page.
  4. Live features: auto-suggest search, form validation, chat, notifications, infinite scroll, live scores.
  5. Desktop-like feel: enables single-page applications (Gmail, Google Maps).

Code example (fetch API)

<button id="load">Load user</button>
<div id="result"></div>

<script>
document.getElementById('load').addEventListener('click', async () => {
  const box = document.getElementById('result');
  box.textContent = 'Loading...';
  try {
    const res  = await fetch('https://jsonplaceholder.typicode.com/users/1');  // async request
    if (!res.ok) throw new Error('HTTP ' + res.status);
    const user = await res.json();                                             // parse JSON
    box.innerHTML = `<b>user.name</b><br>{user.name}</b><br>{user.email}`;                    // update the page
  } catch (err) {
    box.textContent = 'Error: ' + err.message;
  }
});
</script>

Classic version with XMLHttpRequest

const xhr = new XMLHttpRequest();
xhr.open('GET', 'data.php?id=1', true);        // true = asynchronous
xhr.onreadystatechange = function () {
  if (xhr.readyState === 4 && xhr.status === 200) {
    document.getElementById('result').innerHTML = xhr.responseText;
  }
};
xhr.send();

How the page is updated with the response

  1. An event (click, keypress, timer) triggers a JavaScript function.
  2. JavaScript sends an asynchronous request to the server (fetch/XMLHttpRequest).
  3. The server processes it and returns data (JSON, XML, or HTML fragment).
  4. A callback (or await) receives the response once it arrives; the page stays usable meanwhile.
  5. JavaScript parses the data (res.json()) and modifies the DOM (textContent, innerHTML, appendChild) to show it.

Note: innerHTML with untrusted data risks XSS, so prefer textContent or sanitize the data.

Similar questions

Official AppAndroid App

PYQDeck for Android

Solve BEU previous-year exam papers anywhere, anytime. Fast search, offline paper viewing, and step-by-step solutions designed specifically for your phone.