Unit II Ajax
Unit II Ajax
Unit II Ajax
Unit-II (Part-II)
Unit II: Ajax(Part-II)
Introduction to AJAX
XMLHTTP
Ajax Request
Ajax Response
Ajax Events
Ajax XML file
Introduction to Ajax
AJAX stands for Asynchronous JavaScript and XML. AJAX
is a new technique for creating better, faster, and more
interactive web applications with the help of XML, HTML,
CSS, and Java Script.
Ajax uses XHTML for content, CSS for presentation, along
with Document Object Model and JavaScript for dynamic
content display.
Conventional web applications transmit information to and
from the sever using synchronous requests. It means you fill
out a form, hit submit, and get directed to a new page with
new information from the server.
With AJAX, when you hit submit, JavaScript will make a
request to the server, interpret the results, and update the
current screen. In the purest sense, the user would never
know that anything was even transmitted to the server.
XML is commonly used as the format for receiving server
data, although any format, including plain text, can be used.
AJAX is a web browser technology independent of web
server software.
A user can continue to use the application while the client
program requests information from the server in the
background.
Intuitive and natural user interaction. Clicking is not
required, mouse movement is a sufficient event trigger.
Data-driven as opposed to page-driven.
Rich Internet Application Technology
AJAX is the most viable Rich Internet Application
(RIA) technology so far. It is getting tremendous
industry momentum and several tool kit and
frameworks are emerging. But at the same time,
AJAX has browser incompatibility and it is
supported by JavaScript, which is hard to maintain
and debug.
AJAX is Based on Open Standards
AJAX is based on the following open standards −
Browser-based presentation using HTML and
Cascading Style Sheets (CSS).
Data is stored in XML format and fetched from the
server.
Behind-the-scenes data fetches using
XMLHttpRequest objects in the browser.
JavaScript to make everything happen.
AJAX cannot work independently. It is used in combination with
other technologies to create interactive webpages.
JavaScript
Loosely typed scripting language.
JavaScript function is called when an event occurs in a page.
Glue for the whole AJAX operation.
DOM
API for accessing and manipulating structured documents.
Represents the structure of XML and HTML documents.
CSS
Allows for a clear separation of the presentation style from the
content and may be changed programmatically by JavaScript
XMLHttpRequest
JavaScript object that performs asynchronous interaction with the
server.
AJAX = Asynchronous JavaScript And XML.
AJAX is not a programming language.
AJAX just uses a combination of:
A browser built-in XMLHttpRequest object (to request
data from a web server)
JavaScript and HTML DOM (to display or use the data)
AJAX is a misleading name. AJAX applications
might use XML to transport data, but it is equally
common to transport data as plain text or JSON text.
AJAX allows web pages to be updated
asynchronously by exchanging data with a web server
behind the scenes. This means that it is possible to
update parts of a web page, without reloading the
whole page.
Ajax working Model diagram
An event occurs in a web page (the page is loaded,
a button is clicked)
An XMLHttpRequest object is created by
JavaScript
The XMLHttpRequest object sends a request to a
web server
The server processes the request
The server sends a response back to the web page
The response is read by JavaScript
Proper action (like page update) is performed by
JavaScript
Sample Example of Ajax
<!DOCTYPE html>
<html>
<body>
<div id="demo">
<h1>The XMLHttpRequest Object</h1>
<button type="button"
Content</button>
</div>
<script>
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp. {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML =
this.responseText;
}
};
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();
}
</script>
</body>
</html>
Ajax -XMLHTTP
The XMLHttpRequest object is the key to AJAX. It has been
available ever since Internet Explorer 5.5 was released in July
2000, but was not fully discovered until AJAX and Web 2.0 in
2005 became popular.
XMLHttpRequest (XHR) is an API that can be used by
JavaScript, JScript, VBScript, and other web browser scripting
languages to transfer and manipulate XML data to and from a
webserver using HTTP, establishing an independent connection
channel between a webpage's Client-Side and Server-Side.
The data returned from XMLHttpRequest calls will often be
provided by back-end databases. Besides XML,
XMLHttpRequest can be used to fetch data in other formats,
e.g. JSON or even plain text.
The XMLHttpRequest Object
All modern browsers support the
XMLHttpRequest object.
The XMLHttpRequest object can be used to
exchange data with a server behind the scenes.
This means that it is possible to update parts of a
web page, without reloading the whole page.
The XMLHttpRequest object is used to exchange
data with a server.
Create an XMLHttpRequest Object
All modern browsers (Chrome, Firefox, IE7+,
Edge, Safari Opera) have a built-in
XMLHttpRequest object.
Syntax for creating an XMLHttpRequest object:
Property Description
Example
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp. {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML =
this.responseText;
}
};
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();
}
The onreadystatechange event is triggered four times (1-4), one
time for each change in the readyState.
Using a Callback Function
A callback function is a function passed as a parameter
to another function.
If you have more than one AJAX task in a website, you
should create one function for executing the
XMLHttpRequest object, and one callback function for
each AJAX task.
The function call should contain the URL and what
function to call when the response is ready.
Example
loadDoc("url-1", myFunction1);
loadDoc("url-2", myFunction2);
function loadDoc(url, cFunction) {
var xhttp;
xhttp=new XMLHttpRequest();
xhttp. {
if (this.readyState == 4 && this.status == 200) {
myFunction1(this);
}
};
xhttp.open("GET", url, true);
xhttp.send();
}
function myFunction1(xhttp) {
// action goes here
}
function myFunction2(xhttp) {
// action goes here
}
Ajax Response
Server Response Properties
Property Description
Method Description