<script type="text/javascript">
function dataCheck(){
var request=ajaxFunction();
//得到页面上的值
var username=document.getElementById("username").value;
var password=document.getElementById("password").value;
var url = "./login.do?username="+username+"&password="+password;
request.onreadystatechange=function(){
if(request.readyState==4 && request.status==200){
//得到后台的返回值
var result = request.responseText;
if(result!="success"){
alert(message);
}else{
document.forms[0].submit();
}
}
}
//发送到后台的处理链接url
request.open("GET",url,true);
request.send();
}
function ajaxFunction(){
var xmlHttp;
// IE7+, Firefox, Chrome, Opera, Safari
try{
xmlHttp=new XMLHttpRequest();
}catch (e){
// Internet Explorer
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}catch (e){
// IE6, IE5
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){
alert("ajax is not supportted with your Explorer!");
return false;
}
}
}
return xmlHttp;
}
</script>
如果后台使用action1.x来处理,则在返回时可以使用以下方式
response.setContentType("text/html; charset=UTF-8");
response.getWriter().print(result);