AJAX and XMLHTTPRequest
AJAX and XMLHTTPRequest
AJAX and XMLHTTPRequest
Used in Ajax, and to get files and data from the server.
The XMlHTTPRequest object(Cont.)
Some of the object's properties and methods:
oProperties:
Property Description
readyState Integer reporting the status of the request
Determines which event handler will be
called when the object's readyState
onreadystatechange property changes
Data returned by the server expressed as a
responseXML
document object
Data returned by the server in text string
responseText
form
Returns the status as a number (e.g. 404
status
for "Not Found" or 200 for "OK")
The XMlHTTPRequest object(Cont.)
oMethods:
Method Description
abort() Stops the current request.
Specifies the type of request, the URL, and if the
request should be handled asynchronously or not.
o Old versions of Internet Explorer (IE5 and IE6) uses an ActiveX Object:
xmlhttp.open(“get",“json_info.txt",true);
xmlhttp.send();
xmlhttp.open(“get",“/[email protected]",true);
xmlhttp.send();
xmlhttp.open(“post",“/checkEmail.aspx ",true);
xmlhttp.send(“[email protected]”);
Monitoring Request Status
Monitoring RequestStatus:
oWhen a request to a server is sent, we want to perform some actions based
on the response.
oThe onreadystatechange event is triggered every time the readyState
changes.
oThe readyState property holds the status of the XMLHttpRequest.
Monitoring Request Status (Cont.)
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var str=xmlhttp.responseText;
}
}
xmlhttp.open("GET",“json_info.txt",true);
xmlhttp.send();
Dealing with the server response
Complete code:
var xmlhttp;
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var str=xmlhttp.responseText;
}
}
xmlhttp.open("GET",“json_info.txt",true);
xmlhttp.send();
References …
<script>document.writeln(“Thank
You!”)</script>