2081 8

BIT251 ยท 2081 ยท 5 marks

What is AJAX? Write a program to demonstrate the AJAX effect of your choice. [2+3]

What is AJAX? Write a program to demonstrate the AJAX effect of your choice. [2+3]

Web Technology IChapter 9: JavaScript Advanced FeaturesAJAX concept and implementation

Quick answer

AJAX stands for Asynchronous JavaScript and XML. It is a web development technique that allows a web page to communicate with a server and update parts of the page without reloading the entire page.

AJAX: Definition and Demonstration

What is AJAX? [2 marks]

Key characteristics of AJAX:

  • Asynchronous: The browser does not wait for the server response; other operations continue normally.
  • Uses the built-in XMLHttpRequest object (or modern fetch API) to send and receive data from a server in the background.
  • Data can be exchanged in formats like XML, JSON, plain text, or HTML.
  • Provides a faster, smoother user experience by updating only the required portion of the webpage.

AJAX Program: Fetch and Display a Message on Button Click [3 marks]

File Structure

project/
  index.html
  data.txt        (server-side file to fetch)

data.txt

Hello! This message was loaded using AJAX without page reload.

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>AJAX Demo</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 50px;
        }
        #result {
            margin-top: 20px;
            padding: 15px;
            background-color: #e0f7fa;
            border: 1px solid #00acc1;
            display: none;
            width: 400px;
        }
        button {
            padding: 10px 20px;
            background-color: #00acc1;
            color: white;
            border: none;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #007c91;
        }
    </style>
</head>
<body>

    <h2>AJAX Demonstration</h2>
    <p>Click the button to load content without refreshing the page.</p>

    <button onclick="loadContent()">Load Message</button>

    <div id="result"></div>

    <script>
        function loadContent() {
            // Step 1: Create XMLHttpRequest object
            var xhr = new XMLHttpRequest();

            // Step 2: Define what happens when response is received
            xhr.onreadystatechange = function() {
                // readyState 4 = request finished
                // status 200 = OK (success)
                if (xhr.readyState == 4 && xhr.status == 200) {
                    var resultDiv = document.getElementById("result");
                    resultDiv.style.display = "block";
                    resultDiv.innerHTML = "<strong>Server Response:</strong> " 
                                         + xhr.responseText;
                }
            };

            // Step 3: Open a GET request to the server file
            xhr.open("GET", "data.txt", true); // true = asynchronous

            // Step 4: Send the request
            xhr.send();
        }
    </script>

</body>
</html>

How It Works (Step-by-Step)

StepDescription
1User clicks the "Load Message" button
2JavaScript creates an XMLHttpRequest object
3A GET request is sent asynchronously to data.txt on the server
4When the server responds (readyState == 4, status == 200), the response text is displayed inside the <div id="result">
5The page is NOT reloaded -- only the <div> content is updated

Output

When the button is clicked, the following appears on the page without any page refresh:

Server Response:
Hello! This message was loaded using AJAX without page reload.

Note: This program must be run on a web server (e.g., Apache, XAMPP, or VS Code Live Server) because AJAX requests cannot access local files directly due to browser security restrictions.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.