Module 7-AJAX
Module 7-AJAX
APPLICATIONS
MODULE 7 – Ajax-
SYLABUS
• Introduction
• advantages & disadvantages
• Purpose of it
• ajax based web application
• alternatives of ajax
JavaScript in Modern Times-
Introduction AJAX
HTML
Used to build web forms and identify fields
Javascript
Facilitates asynchronous communication and modification
of HTML in-place
DHTML - Dynamic HTML
Additional markup for modifying and updating HTML
DOM - Document Object Model
Used via Javascript to work with both the structure of your
HTML and also XML from the server
How AJAX Works:
• User Interaction:
• A user interacts with a web page (e.g., clicks a button).
• AJAX Request:
• JavaScript makes an HTTP request to the server (using XMLHttpRequest or
the newer fetch API).
• Server Response:
• The server processes the request and sends back data (often in JSON format).
• Update Web Page:
• JavaScript processes the server's response and updates the web page without
reloading.
Asynchronous data requests
The better AJAX way
Ajax based web application
What is AJAX?
Example
var xhttp = new XMLHttpRequest();
XMLHttpRequest Object Methods
Method Description
new XMLHttpRequest() Creates a new XMLHttpRequest object
abort() Cancels the current request
getAllResponseHeaders() Returns header information
getResponseHeader() Returns specific header information
open(method,url,async,user,psw) Specifies the request
Property Description
onreadystatechange Defines a function to be called when the readyState property changes
readyState Holds the status of the XMLHttpRequest.
0: request not initialized
1: server connection established
2: request received
3: processing request
4: request finished and response is ready
responseText Returns the response data as a string
responseXML Returns the response data as XML data
status Returns the status-number of a request
200: "OK"
403: "Forbidden"
404: "Not Found"
For a complete list go to the Http Messages Reference
statusText Returns the status-text (e.g. "OK" or "Not Found")
Send a Request To a Server
To send a request to a server, we use the open() and send()
methods of the XMLHttpRequest object:
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();
Method Description
open(method, Specifies the type of request
url, async)
method: the type of request: GET or POST
url: the server (file) location
async: true (asynchronous) or false
(synchronous)
<div id="demo">
<h2>Let AJAX change this text</h2>
<button type="button" onclick="loadDoc()">Change
Content</button>
</div>
</body>
</html>
The function requests data from a web server and displays it:
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200)
{
document.getElementById("demo").innerHTML = th
is.responseText;
}
};
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();
}
The "ajax_info.txt" file used in the example above, is
a simple text file and looks like this:
<h1>AJAX</h1>
<p>AJAX is not a programming language.</p>
<p>AJAX is a technique for accessing web servers
from a web page.</p>
<p>AJAX stands for Asynchronous JavaScript And
XML.</p>
AJAX has both advantages and
disadvantages:
Advantages :