ajax的验证用户是否正确的,,有点不明白
<html>
<head>
<title>judegUsername.axpx</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script language="JavaScript" type="text/javascript">
var request = false;
try{
request = new XMLHttpRequest();
}catch(microsoftIE){
try{
request = new ActiveXObject("Msxml2.XMLHTTP");
}catch(microsoftOldIE){
try{
request = new ActiveXObject("Microsoft.XMLHTTP");
}catch(failed){
request = false;
}
}
}
if(!request){
alert("初始化XMLHttpRequest对象失败");
}
function sendUsername(){
var username = document.getElementById("username").value;
if(username!=null && username!=""){
var url = "./judgeUsername.axpx?username="+escape(username);
request.open("get",url,true);
request.onreadystatechange=updatepage;
request.send(null);
}
}
function updatepage(){
if(request.readyState==4){
if(request.status==200){
document.getElementById("reusername").innerHTML = request.responseText;
//document.getDocumentById("reusername").innerText = request.responseText;
}else if(request.status==404){
alert("找不到页面!");
}else{
alert("错误代码:"+request.status);
}
}
}
function clearMessage(){
document.getElementById("reusername").innerHTML = "";
}
</script>
</head>
<body>
<form action="/register.jsp" method="post">
<table align="center">
<caption>guoyongfei为已注册,其他均未注册</caption>
<tr><th>用 户 名:</th><td><input type="text" name="username" id="username" onblur="sendUsername()" onfocus="clearMessage()"/></td><td width="40%" id="reusername"></td></tr>
<tr><th style="height: 26px">密 码:</th><td style="height: 26px"><input type="password" name="password" id="password"/></td><td width="20" style="height: 26px"></td></tr>
<tr><th>密码确认:</th><td><input type="text" name="surePassword" id="surePassword"/></td><td width="20"></td></tr>
</table>
</body>
</html>
在另一个弹出页面 judgeUsername.axpx 的page_load:
//这里要写什么?
//System.out.println("username:"+username);
if (username.Equals ("guoyongfei"))
{
Response.Write("<font color='red'>用户名已经注册</font>");
}
else
{
Response.Write("<font color='green'>用户名可以用</font>");
}
[解决办法]
给楼主一个参考
1.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head> <title>Register</title><script type="text/javascript"> //声明一个全局变量,用来保存XMLHttpRequest对象的引用(未初始化) var xmlHttp = false; //创建XMLHttpRequest对象函数 function GetXMLHttpRequest() { // 由于 XMLHttpRequest 对象在各个浏览器中实现机制不同,所以要综合考虑 var xmlHttp = false; //如果是Mozilla浏览器 if (!xmlHttp && typeof XMLHttpRequest != 'object') //undefined { xmlHttp = new XMLHttpRequest(); } //如果是IE浏览器 else if(window.ActiveXObject) { try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e2) { xmlHttp = false; } } } return xmlHttp; } //当用户名文本框失去输入焦点时响应 function checkUserName() { // 检查文本框是否有输入 var txtUserName = document.getElementById('txtUserName'); if (txtUserName.value == "") return; xmlHttp = GetXMLHttpRequest(); // 构造将XMLHttpRequest对象将请求的服务器端的URL字符串 var url = "CheckUserName.aspx?UserName=" + txtUserName.value; // 建立与服务器端的连接 xmlHttp.open("GET", url, true); // 设置回调函数 xmlHttp.onreadystatechange = callBack_CheckUserName; // 发送请求 xmlHttp.send(null); } //处理响应 function callBack_CheckUserName() { var lblMessage = document.getElementById("lblMessage"); // 4代表服务器端执行完毕并已经成功返回 if (4 == xmlHttp.readyState) { if(200 == xmlHttp.status) { //通过responseText属性得到服务器端实际返回的文本 var isValid = xmlHttp.responseText; //根据isValid的值决定lblNameMessage的显示内容 lblMessage.innerHTML = (isValid.toLowerCase() == "true") ? "用户名可用" : "用户名已被注册"; } } else { lblMessage.innerHTML = "数据查询中..."; } } </script> </HEAD><body><form method="post" action=""> <div> 用户名: <input type="text" id="txtUserName" name="txtUserName" onblur="return checkUserName();"/> <span id="lblMessage"></span><br> 密 码: <input type="password" id="txtPassword" name="txtPassword" /><br> <input type="submit" id="btnRegister" name="btnRegister" value="注册" /> </div></form></body></html>