ajax調(diào)用java方法(2)
ajax調(diào)用java方法(三)
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("異步通信對象創(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)
{
//這是返回的結果
var rText=window.http_request.responseText;
//處理結果
if(rText=="true")
{
alert("用戶名稱已經(jīng)存在");
}else{
alert("用戶名稱可以使用");
}
}
}
}
//調(diào)用
sendRequest("POST","user.do","userName="+name,getResult);