Inorder to make an asynchronous call to a server we need to :
1.) create a XMLHttpRequest object.
2.) (a) check the state of the 'readyState' property of the created XMLHttpRequest object. if this value is '4' (numeric 4), it means that the processing is complete and the response returned from the server is ready for use.
(b) assign a javascript function to 'onreadystatechange' method. This javascript function is called whenever the servers reponse is complete and then the data can be processed.
The code
var xmlHttp;
try
{ // Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{ // Internet Explorer try
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.myForm.time.value = xmlHttp.responseText;
}
}
deals will the above 2 points.
There are two ways to pass parameters using XMLHttpRequest object.
1.) appending the parameters to the end of the url.
on the client side we need to write the code as shown below, where 'elementID' is a parameter
xmlHttp.open("GET","ajax.do?elementID='this is an element'",true);
on the server side we read the values as
HttpServletRequest.getParameter("elementID");
or
HttpServletRequest.getHeader("elementID");
with the "GET" action both 'getParameter' and 'getHeader' methods work.
2.) using the 'setRequestHeader' method of XMLHttpRequest object.
on the client side we need to write the code below, where 'elementID' is a parameter and 'testajax' is the java servlet
xmlHttp.open("POST","testajax",true);
xmlHttp.setRequestHeader("elementID" , "xxxxyyyyzzzz");
on the server side we read the values as
HttpServletRequest.getHeader("elementID");
with the "POST" action ONLY 'getHeader' method works.
1.) create a XMLHttpRequest object.
2.) (a) check the state of the 'readyState' property of the created XMLHttpRequest object. if this value is '4' (numeric 4), it means that the processing is complete and the response returned from the server is ready for use.
(b) assign a javascript function to 'onreadystatechange' method. This javascript function is called whenever the servers reponse is complete and then the data can be processed.
The code
var xmlHttp;
try
{ // Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{ // Internet Explorer try
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.myForm.time.value = xmlHttp.responseText;
}
}
deals will the above 2 points.
There are two ways to pass parameters using XMLHttpRequest object.
1.) appending the parameters to the end of the url.
on the client side we need to write the code as shown below, where 'elementID' is a parameter
xmlHttp.open("GET","ajax.do?elementID='this is an element'",true);
on the server side we read the values as
HttpServletRequest.getParameter("elementID");
or
HttpServletRequest.getHeader("elementID");
with the "GET" action both 'getParameter' and 'getHeader' methods work.
2.) using the 'setRequestHeader' method of XMLHttpRequest object.
on the client side we need to write the code below, where 'elementID' is a parameter and 'testajax' is the java servlet
xmlHttp.open("POST","testajax",true);
xmlHttp.setRequestHeader("elementID" , "xxxxyyyyzzzz");
on the server side we read the values as
HttpServletRequest.getHeader("elementID");
with the "POST" action ONLY 'getHeader' method works.
No comments:
Post a Comment