Chapter 8 Ajax
Chapter 8 Ajax
Chapter 8 Ajax
AJAX
What is AJAX?
❑AJAX = Asynchronous JavaScript and XML.
❑AJAX is a technique for creating fast and dynamic web
pages.
❑AJAX allows web pages to be updated asynchronously by
exchanging small amounts of data with the server behind
the scenes -, and updating parts of a web page - without
reloading the whole page.
❑ Classic web pages must reload the entire page if the
content should change.
AJAX Example
example1
With AJAX, 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 when the response ready
The file can be any kind of file, like .txt and .xml, or
server scripting files like .asp and .php (which can
perform actions on the server before sending the
response back).
Async=true
When using async=true, specify a function to execute when
the response is ready in the onreadystatechange event:
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 &&xmlhttp.status==200){
document.getElementById("myDiv").innerHTML=xmlhttp.response
Text;
}
}
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
Async=false
❑ To use async=false, change the third parameter in the open()
method to false:
xmlhttp.open("GET","ajax_info.txt",false);
GET Requests
xmlhttp.open("GET","demo_get.php",true);
xmlhttp.send();
example2
example2b
POST Requests
To POST data like an HTML form, add an HTTP header with
setRequestHeader(). Specify the data you want to send in
the send() method:
Method Description
setRequestHeader(header,value) Adds HTTP headers to the
request.
POST Requests
xmlhttp.open("POST","demo_post.php",true);
xmlhttp.send();
example3
xmlhttp.open("POST","ajax_test.php",true);
xmlhttp.setRequestHeader("Content-type",
"application/x-www-form-
urlencoded");
xmlhttp.send("fname=Henry&lname=Ford");
example3b
XMLHttpRequest Properties
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
xmlhttp.onreadystatechange=function(){
// Get the data from the server's response
if (xmlhttp.readyState==4 &&xmlhttp.status==200) {
document.getElementById("myDiv").innerHTML
=xmlhttp.responseText;
}
}
Server Response
To get the response from a server, use the responseText or
responseXML property of the XMLHttpRequest object.
Property Description
responseText get the response data as a string
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
❑If you have more than one AJAX task on your website,
you should create ONE standard function for creating the
XMLHttpRequest object, and call this for each AJAX task.
function myFunction(){
loadXMLDoc("ajax_info.txt",function() {
if (xmlhttp.readyState==4 &&xmlhttp.status==200) {
document.getElementById("myDiv").innerHTML=xmlhttp.responseT
ext;
}
});
}
example5
example6
When a user types a character in the input field above,
the function "showHint()" is executed. The function is
triggered by the "onkeyup" event:
The page on the server called by the JavaScript above is an PHP file called
"gethint.php".
Refer to the gethint.php for the details
example7
jQuery - AJAX
Example1
$("#div1").load("demo_test.txt");
Example1
$("#div1").load("demo_test.txt #p1");
example1b
example1c
AMIT2043 Web Systems and Technologies
Chapter 8 : AJAX
$("button").click(function(){
$.post("demo_test_post.asp",
{
name:"Donald Duck",
city:"Duckburg"
},
function(data,status){
alert("Data: " + data + "\nStatus: " + status);
});
});
example2b
The End