Multimedia and Advance Web Technologies
By:
Dr. Mohammad Shoab
Week 12
What is Ajax?
Multimedia and Advance Web Technologies
Department of Computer Science
What is so cool about Ajax?
Multimedia and Advance Web Technologies
Department of Computer Science
How does Ajax work?
Ajax isn’t a technology. It’s really several technologies, each flourishing in its own right, coming together in powerful new ways.
Ajax incorporates:
Multimedia and Advance Web Technologies
Department of Computer Science
Classic VS Ajax
Multimedia and Advance Web Technologies
Department of Computer Science
How is Ajax Different?
Multimedia and Advance Web Technologies
Department of Computer Science
Synchronous web communication
CS380
7
Multimedia and Advance Web Technologies
Department of Computer Science
Asynchronous web communication
CS380
8
Multimedia and Advance Web Technologies
Department of Computer Science
The XMLHttpRequest Object
Multimedia and Advance Web Technologies
Department of Computer Science
Send a Request To a Server
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();
Method | Description |
open(method, url, async) | Specifies the type of request��method: the type of request: GET or POST�url: the server (file) location�async: true (asynchronous) or false (synchronous) |
send() | Sends the request to the server (used for GET) |
send(string) | Sends the request to the server (used for POST) |
Multimedia and Advance Web Technologies
Department of Computer Science
Server Response
Property | Description |
responseText | get the response data as a string |
responseXML | get the response data as XML data |
Multimedia and Advance Web Technologies
Department of Computer Science
The responseText Property
document.getElementById("demo").innerHTML = xhttp.responseText;
Multimedia and Advance Web Technologies
Department of Computer Science
The responseXML Property
Request the file cd_catalog.xml and parse the response:
xmlDoc = xhttp.responseXML;� txt = "";� x = xmlDoc.getElementsByTagName("ARTIST");� for (i = 0; i < x.length; i++) {� txt += x[i].childNodes[0].nodeValue + "<br>";� }� document.getElementById("demo").innerHTML = txt;
Multimedia and Advance Web Technologies
Department of Computer Science
AJAX - Events
The onreadystatechange event
Property | Description |
onreadystatechange | Stores a function (or the name of a function) to be called automatically each time the readyState property changes |
readyState | Holds the status of the XMLHttpRequest. Changes from 0 to 4: �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 |
Multimedia and Advance Web Technologies
Department of Computer Science
Simple Example
<!DOCTYPE html>
<html>
<body>
<h2>AJAX</h2>
<button type="button" onclick="loadDoc()">Request data</button>
<p id="demo"></p>
<script>
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("demo").innerHTML = xhttp.responseText;
}
};
xhttp.open("GET", "demo_get.asp", true);
xhttp.send();
}
</script>
</body>
</html>
Multimedia and Advance Web Technologies
Department of Computer Science
The End
16
Multimedia and Advance Web Technologies
Department of Computer Science
Exercise
Q1. What is AJAX?
Q2. How AJAX works?
Q3. How AJAX is different?
Q4. Explain XMLHttpRequest object.
Q5. What are responseText and responseXML property?
Q6. Explain AJAX events.
17
Multimedia and Advance Web Technologies
Department of Computer Science
Q7. AJAX is about updating parts of a
Q8. AJAX allows web pages to be updated
18
Multimedia and Advance Web Technologies
Department of Computer Science
Q9. The keystone of AJAX is the
Q10. The responseText property returns the response as a
19
Multimedia and Advance Web Technologies
Department of Computer Science