LM Unit4
LM Unit4
Where it is used?
There are too many web applications running on the web that are using AJAX
Technology. Some are : 1. Gmail 2. Face book 3. Twitter 4. Google maps 5. YouTube
etc.,
Working of AJAX
Traditional web applications are created by adding loosely web pages through links in
a predefined order. Where the user can move from one page to another page to
interact with the different portions of the applications.
Also, HTTP requests are used to submit the web server in response to the user action.
On receiving the request, the web server fulfills the request by returning a new
webpage which, then displays on the web browser. This process includes lots of pages
refreshing and waiting.
AJAX change this whole working model by sharing the minimum amount of data
between the web browser and server asynchronously. It speedup up the working
of the web applications.
It creates an additional layer known as AJAX engine in between the web application
and webserver due to which we can make background server calls using JavaScript
and retrieve the required data, can update the requested portion of a web page
without casing full reload of the page.
Asynchronous processes reduce the workload of the web server by dividing the work
with the client computer. Due to the reduced workload web servers become more
responsive and fast.
Apart from XML, XMLHttpRequest can also fetch data in various formats like JSON, etc.
It creates an asynchronous connection between the client side and the server side.
Syntax
variableName = new XMLHttpRequest()
Where using a new keyword along with XMLHttpRequest() constructor we can be able
to create a new XMLHttpRequest object. This object must be created before calling
the open() function to initialise it before calling send() function to send the request
to the web server.
▪ To do the request, we need 3 steps:
1. Create XMLHttpRequest:
let xhr = new XMLHttpRequest();
o The constructor has no arguments.
2. Initialize it, usually right after new XMLHttpRequest: it is open() for
configure the request only.
xhr.open(method, URL, [async, user, password])
o This method specifies the main parameters of the request:
✓ method – HTTP-method. Usually "GET" or "POST".
✓ URL – the URL to request, a string, can be URL object.
✓ async – if explicitly set to false, then the request is
synchronous, we’ll cover that a bit later.
✓ user, password – login and password for basic HTTP auth (if
required).
3. Send it out. :to start activity call send()
xhr.send([body])
o This method opens the connection and sends the request to server.
The optional body parameter contains the request body.
o Some request methods like GET do not have a body. And some of
them like POST use body to send the data to the server.
Examples:
onload
▪ With the XMLHttpRequest object we can define a callback function to be
executed when the request receives an answer.
▪ The function is defined in the onload property of
the XMLHttpRequest object:
<!DOCTYPE html>
<html>
<body>
<div id="demo">
<h2>The XMLHttpRequest Object</h2>
<button type="button" onclick="loadDoc()">Change Content</button>
</div>
<script>
function loadDoc() {
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
document.getElementById("demo").innerHTML =
this.responseText;
}
xhttp.open("GET", "ajax_info.txt");
xhttp.send();
}
</script>
</body>
</html>
Onreadystatechange:
▪ The readyState property holds the status of the XMLHttpRequest.
▪ The onreadystatechange property defines a callback function to be
executed when the readyState changes.
Data.txt
Index.html
<!DOCTYPE html>
<html lang="en">
<head>
<script src="jquery.min.js"></script>
</head>
<body>
<div id="d1"></div>
<script>
$(document).ready(function () {
$("#btn").click(function () { // Use # for ID selectors
$.getJSON("data1.json", function (data) {
$("#d1").empty(); // Clear previous content
for (let i = 0; i < data.length; i++) {
$("#d1").append("<p>" + data[i].name + "</p>"); // Append names in <p> tags
}
}).fail(function () {
$("#d1").text("Error: Unable to load data.");
});
});
});
</script>
</body>
</html>
jquery.ajax() or $.ajax()
▪ The ajax() method in jQuery is used to perform an AJAX request or
asynchronous HTTP request.
Data.txt
<TABLE BORDER="1">
<tr><td>Name </td><td> Contact No</td> </tr>
<tr><td>Ram </td><td> 999952255</td> </tr>
<tr><td>Nila </td><td> 888545221</td> </tr>
<tr><td>Raj </td><td> 784555622</td> </tr>
</TABLE>
Index.html
<html>
<head>
<script src="jquery.min.js"></script>
</head>
<body>
<h1>text -> jQuery</h1>
<button id="textbtn">Load Text File</button>
<div id="content" style="margin-top: 20px; border: 1px solid #ccc;
padding: 10px;"></div>
<script>
$(document).ready(function () {
$("#textbtn").click(function () {
$.ajax({
url: "data.txt", // Path to your text file
type: "GET",