2 AJAX (Unit 3)
2 AJAX (Unit 3)
Introduction to AJAX
Working of AJAX
Purpose of AJAX
Data Exchange in AJAX
History of AJAX
Structure and C/S Process
Pros and Cons of AJAX
What is AJAX ?
• AJAX = Asynchronous Javascript and XML.
• Not a stand-alone language or technology.
• 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)
• It combines a set of known technologies in order to create faster and more
user friendly web pages.
• It is a client side technology.
• Read data from a web server - after the page has loaded
• Update a web page without reloading the page
• Send data to a web server - in the background
Technologies Used
AJAX uses:
• Javascript (for altering the page)
• XML (for information exchange)
• ASP or JSP (server side)
Data Exchange in AJAX
• In AJAX:
C/S Processes
Most of the time client requires two files
Html page: handles GUI and calls a function of JavaScript.
JavaScript: handles the business logic of the system
In JavaScript, a request is sent by client and response is taken
from server via XMLHTTP object
Response of server should be returned in xml file structure
Only requested data is returned
XMLHTTP Object
• The object that is used to connect to the remote server is called XMLHTTP.
• It resembles the Java’s URL object that is used to access a specific URL and get the
contents.
For IE 5.5:
objXmlHttp=new ActiveXObject(“Microsoft.XMLHTTP”)
For Mozilla:
objXmlHttp=new XMLHttpRequest()
Sending and Retrieving information
• After creating the object, we can send information to the web server and get
the answer using this object’s functions:
• GET METHOD
• POST METHOD
Retrieving information
We get the returned value with the property:
“xmlHttp.responseText”.
Perform behind the scenes to update portions of a webpage without having to reload the
whole page. Based on:
readyState statuses:
0: request not initialized
1: server connection established
2: request received
3: processing request
4: request finished and response is ready
status:
200 = "OK"
404 = Page not found
Create a simple XMLHttpRequest, and retrieve data from a TXT file
• Disadvantages:
– The back button problem. People think that when they press
back button, they will return to the last change they made, but
in AJAX this doesn not hold.
– Possible network latency problems. People should be given
feedback about the processing.
– Does not run on all browsers.