AJAX
1. What is AJAX?
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 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.
2. How AJAX Works?
1. An event occurs in a web page (the page is loaded, a button is clicked)
2. An XMLHttpRequest object is created by JavaScript
3. The XMLHttpRequest object sends a request to a web server
4. The server processes the request
5. The server sends a response back to the web page
6. The response is read by JavaScript
7. Proper action (like page update) is performed by JavaScript.
3. Write a note on XMLHttpRequest Object
The XMLHttpRequest Object
XMLHttpRequest object is the keystone of AJAX
All modern browsers support the XMLHttpRequest object.
The XMLHttpRequest object can be used to exchange data with a server. This means that it is
possible to update parts of a web page, without reloading the whole page.
Create an XMLHttpRequest Object
All modern browsers (Chrome, Firefox, Edge (and IE7+), Safari, Opera) have a built-in
XMLHttpRequest object.
Syntax for creating an XMLHttpRequest object:
variable = new XMLHttpRequest();
Example:
var xhttp = new XMLHttpRequest();
Refer to example program in the last page
XMLHttpRequest Object Methods
XMLHttpRequest Object Properties
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
status 200: "OK"
403: "Forbidden"
404: "Page not found"
statusText Returns the status-text (e.g. "OK" or "Not Found")
4. Explain the process of sending request to a server in AJAX
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();
GET or POST?
GET is simpler and faster than POST, and can be used in most cases.
However, always use POST requests when:
A cached file is not an option (update a file or database on the server).
Sending a large amount of data to the server (POST has no size limitations).
Sending user input (which can contain unknown characters), POST is more robust and
secure than GET.
GET Requests
A simple GET request:
xhttp.open("GET", "demo_get.asp", true);
xhttp.send();
POST Requests
A simple POST request:
xhttp.open("POST", "demo_post.asp", true);
xhttp.send();
Asynchronous Request
Server requests should be sent asynchronously.
The async parameter of the open() method should be set to true:
xhttp.open("GET", "ajax_test.asp", true);
By sending asynchronously, the JavaScript does not have to wait for the server response, but
can instead:
execute other scripts while waiting for server response
deal with the response after the response is ready
Synchronous Request
To execute a synchronous request, change the third parameter in the open() method to false:
xhttp.open("GET", "ajax_info.txt", false);
Sometimes async = false are used for quick testing. You will also find synchronous requests in
older JavaScript code.
Refer to example program in the last page
5. Explain the process of receiving response from server in AJAX
The onreadystatechange Property
The readyState property holds the status of the XMLHttpRequest.
The onreadystatechange property defines a function to be executed when the readyState
changes.
The status property and the statusText property holds the status of the XMLHttpRequest
object.
The onreadystatechange function is called every time the readyState changes.
When readyState is 4 and status is 200, the response is ready:
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
status 200: "OK"
403: "Forbidden"
404: "Page not found"
statusText Returns the status-text (e.g. "OK" or "Not Found")
Server response properties
Server response methods
The responseText Property
The responseText property returns the server response as a JavaScript string, and you can use
it accordingly:
Example: document.getElementById("demo").innerHTML = xhttp.responseText;
The responseXML Property
The XML HttpRequest object has an in-built XML parser.
The property returns the server response as an XML DOM object.
Using this property you can parse the response as an XML DOM object:
The getAllResponseHeaders() Method
The getAllResponseHeaders() method returns all header information from the server
response.
The getResponseHeader() Method
The getResponseHeader() method returns specific header information from the server
response.
Example program
ajax_info.txt
What are the technologies used by AJAX?
It combines multiple web-related technologies like HTML, XHTML, CSS, JavaScript, DOM, XML,
XSLT and XMLHttpRequest object