Unit 4
Unit 4
Unit 4
AJAX allows you to send and receive data asynchronously without reloading the web page. So it
is fast.
AJAX allows you to send only important information to the server not the entire page. So only
valuable data from the client side is routed to the server side. It makes your application
interactive and faster.
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.
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.
A synchronous request blocks the client until operation completes i.e. browser is unresponsive.
In such case, javascript engine of the browser is blocked.
As in the above image, full page is refreshed at request time and user is blocked until request
completes.
Asynchronous (AJAX Web-Application Model)
An asynchronous request doesn’t block the client i.e. browser is responsive. At that time, user
can perform another operations also. In such case, javascript engine of the browser is not
blocked.
As in the above image, full page is not refreshed at request time and user gets response from
the ajax engine.
AJAX Technologies
The technologies that are used by AJAX are already implemented in all the Morden browsers.
So the client does not require any extra module to run the AJAX application.The technologies
used by AJAX are −
Property Description
When a request to a server is sent, we want to perform some actions based on the response.
Example
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("demo ajax").innerHTML = xhttp.responseText;
}
};
Method Description
void open(method, URL) opens the request specifying get or post method
and url.
void open(method, URL, async, username, same as above but specifies username and
password) password.
open() method will opens the connection with the server and send will sends our request
object to the server.
first parameter having the values POST/GET this is depends on our requirement, my choice
is always POST, because its having security than GET
Second parameter is the destination, to where we need to send the request. It may be
any file path or url or url patterns [ in java ] or what ever
Third parameter having the values true/false, actually true means we are
opening Asynchronous data transfer, and false means Synchronous
Finally send() method will send the request object to the server.
Example:
Example
xhttp.open("GET", "demo_ajax2.asp?fname=sachin&lname=tendulkar", true);
xhttp.send();
How AJAX works?(AJAX Architecture)
AJAX communicates with the server using XMLHttpRequest object. Let's try to understand the
flow of ajax or how ajax works by the image displayed below.
AJAX Architecture
1. User sends a request from the UI and a javascript call goes to XMLHttpRequest object.
2. HTTP Request is sent to the server by XMLHttpRequest object.
3. Server interacts with the database using JSP, PHP, Servlet, ASP.net etc.
4. Data is retrieved.
5. Server sends XML data or JSON data to the XMLHttpRequest callback function.
6. HTML and CSS data is displayed on the browser.