ajax調(diào)用java后臺(tái)方法是什么
ajax調(diào)用java后臺(tái)方法是什么
ajax是一種基于 JavaScript和HTTP請(qǐng)求(HTTP requests),廣泛應(yīng)用在瀏覽器的網(wǎng)頁開發(fā)技術(shù)。那么,ajax調(diào)用java后臺(tái)方法是什么?
var http_request=false;
function sendRequest(method,url,content,processResponse)
{
http_request=false;
if(window.XMLHttpRequest) //mozilla
{
http_request=new XMLHttpRequest();
}else if(window.ActiveXObject) //IE
{
try
{
http_request=new ActiveXObject("Msxml2.XMLHTTP");
}catch(e)
{
http_request=new ActiveXObject("Microsoft.XMLHTTP");
}
}
if(!http_request)
{
alert("異步通信對(duì)象創(chuàng)建失敗!");
return;
}
if(method.toLowerCase()=="get")
{
http_request.open(method,url,true);
http_request.onreadystatechange=processResponse;
http_request.send(content);
return;
}
if(method.toLowerCase()=="post")
{
http_request.open(method,url,true);
http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
http_request.onreadystatechange=processResponse;
http_request.send(content);
return;
}
}
function getResult()
{
if(window.http_request.readyState==4)
{
if(window.http_request.status==200)
{
//這是返回的結(jié)果
var rText=window.http_request.responseText;
//處理結(jié)果
if(rText=="true")
{
alert("用戶名稱已經(jīng)存在");
}else{
alert("用戶名稱可以使用");
}
}
}
}
//調(diào)用
sendRequest("POST","user.do","userName="+name,getResult);