Professional Documents
Culture Documents
AJAX
Overview of Ajax.
The basics of Ajax.
Example programs using GET and POST
method.
03/20/23 Dept of CSE, NMAMIT, Nitte 1
What is AJAX
AJAX = Asynchronous JavaScript And XML.
AJAX is not a programming language.
HTML, Images,
CSS, JavaScript
Data
Web Browser
DataBase
HTTP Request
Query/Data
Request
Web Server
Web Browser
HTML,
CSS Data Data
User Ajax
Interface Engine DataBase
JavaScript Call HTTP
Request Query/Data
Request
Web Server
web server)
JavaScript and HTML DOM (to display or use the data)
For IE,
Example
var xhttp = new XMLHttpRequest();
Method Description
new XMLHttpRequest() Creates a new XMLHttpRequest object
abort() Cancels the current request
getAllResponseHeaders() Returns header information
getResponseHeader() Returns specific header information
open(method,url,async,user,psw) Specifies the request
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
5. Status
200 – OK
404 – Page not found
So when readystate=4 and status =200 then everything will be perfect
means we get the response from the server and out webpage will be
modified with response text/XML.
request
401 (UNAUTHORIZED) – client is not authorized to access the
resource
403 (FORBIDDEN) – client failed to gain authorization
After a request to the server, we need a function that can receive the
data that is returned by the server.
The onreadystatechange property stores the function that will process
will be executed.
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{ // some code here }
}
limitations).
Sending user input (which can contain unknown characters), POST is
Example:
xhttp.open("GET", "demo_get.asp", true);
xhttp.send();
If you want to send information with the GET method, add the
information to the URL:
Example:
xhttp.open("GET", "demo_get2.asp?fname=Henry&lname=Ford", true);
xhttp.send();
xhttp.open("GET", "ajax_test.asp", true);
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).
xhttp.open("GET", "ajax_test.asp", true);
The data sent back from the server can be retrieved with the
responseText property.
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.myForm.time.value=xmlHttp.responseText;
}
}
Code written is much cleaner and the intent of the code is much
more apparent than using numerous callback functions with hidden
frames
User First – Design the Ajax application with the users in mind
before anything else. Try to make common use cases easy to
accomplish the task