CSC329 ยท 2080 ยท 5 marks
What is XMLHttpRequest object? Write HTML script with AJAX showing the use of XMLHttpRequest object.
What is XMLHttpRequest object? Write HTML script with AJAX showing the use of XMLHttpRequest object. [5]
Web TechnologyChapter 5: AJAX and XMLBasics of AJAX
Quick answer
The XMLHttpRequest (XHR) object is a built-in browser object that is used to exchange data with a web server in the background. It is the core component of AJAX technology. Using the XMLHttpRequest object, a web page can request data from a server and update only a portion of the page without reloading the entire page.
XMLHttpRequest Object and AJAX
XMLHttpRequest Object
Syntax to create an XMLHttpRequest object:
var xhr = new XMLHttpRequest();
Key Methods of XMLHttpRequest Object
| Method | Description |
|---|---|
open(method, url, async) | Specifies the type of request (GET/POST), the server URL, and whether the request is asynchronous (true) or synchronous (false) |
send() | Sends the request to the server (used for GET) |
send(string) | Sends the request to the server (used for POST) |
Key Properties of XMLHttpRequest Object
| Property | Description |
|---|---|
responseText | Returns the server response as a JavaScript string |
responseXML | Returns the server response as an XML DOM object |
onreadystatechange | Defines a function to be called when the readyState changes |
readyState | Holds the status of the request (0 to 4) |
status | Returns the HTTP status code (e.g., 200 for OK) |
HTML Script with AJAX Using XMLHttpRequest Object
<!DOCTYPE html>
<html>
<head>
<title>AJAX XMLHttpRequest Example</title>
<script>
function loadData() {
// Step 1: Create XMLHttpRequest object
var xhr = new XMLHttpRequest();
// Step 2: Define what happens when response is received
xhr.onreadystatechange = function() {
// readyState 4 means request is done
// status 200 means response is OK
if (xhr.readyState == 4 && xhr.status == 200) {
// Step 5: Update the page with the server response
document.getElementById("result").innerHTML = xhr.responseText;
}
};
// Step 3: Open the request (GET method, async = true)
xhr.open("GET", "ajax_info.txt", true);
// Step 4: Send the request to the server
xhr.send();
}
</script>
</head>
<body>
<h2>AJAX Demo Using XMLHttpRequest</h2>
<button onclick="loadData()">Click to Load Data</button>
<!-- This div will be updated without page reload -->
<div id="result">
<p>Data will appear here without reloading the page.</p>
</div>
</body>
</html>
How This Script Works (AJAX Working Steps)
- The user clicks the "Click to Load Data" button, which triggers the
loadData()function. - A new XMLHttpRequest object (
xhr) is created. - The
open()method specifies a GET request toajax_info.txtwithasync = true(asynchronous). - The
send()method sends the request to the web server. - When the server responds (
readyState == 4andstatus == 200), theonreadystatechangefunction is called. - The response text from the server is displayed inside the
<div id="result">element without reloading the page.
Note: The file
ajax_info.txtis a simple text file stored on the server that contains the data to be fetched and displayed.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.