AJAX TUTORIAL

What is AJAX?
Ajax stands for Asynchronous Java Script and XML.By AJAX we can update a webpage without reloading the page.It's a techonolgy for craeting a fast and dynamic web page.It combines the java script and XML together.

Here in this tutorial I will tell you how to implement AJAX in JSP Page.

A Simple Ajax Example-
We will enter your name in the texbox and on button click it will show the message welcome to [yourname] to AJAX world without reloading the page.

1.First create a java script file and name this file as ajax.js
write the below code in the ajax.js file.
function createRequestObject(){
var req;
if (window.xmlHttpRequest){
//For Firefox ,Safari and Opera
req=new xmlHttpReuest();
}
else if(window.ActiveXObject){
//For IE 5+
req=new ActiveXObject("Microsoft.XMLHTTP");
}
else{
//Error for an old browser
alert('Your browser is not IE 5 or higher ,or Firefox or Safari or Opera);
}
return req;
}
//Make the XMLHttpRequest object
var http=createRequestObject();
function sendRequest(method,url){
if(method=='post' method=='POST'){
myvar=document.myForm.Countrynameddl.options[document.myForm.countrynameddl.selectedindex].value;
url=url+"?abc="+myvar;
http.open(method,url);
http.onreadystatechange=handleResponse;
http.send(null);
}
}

function handleResponse(){
if(http.readyState== 4 && http.status== 200){
var response=http.responseText;
if(response){
document.getElementById("ajax_res").innerHTML=response;
}
}
}

}

No comments:

Post a Comment