AJAX实现即时用户名的验证

菜鸟笔记公众号 2007-11-27 07:48:43

刚开始接触AJAX,希望高手帮忙!谢谢....

代码如下:

<script type ="text/javascript" language="javascript">
var xmlHttp;
function createXMLHttpRequest() //创建XMLHttpRequest对象
{
if (window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
}
function callServer()
{
createXMLHttpRequest();
var username=document.getElementById("txtusername").value;
if((username==null)||(username=="")) return;
var url="checkname.aspx?name="+escape(username);

xmlHttp.onreadystatechange = updatePage;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
function updatePage()
{
if (xmlHttp.readyState<4) //表示请求状态还没有成功
{
test1.innerHTML="loading....";
}
if(xmlHttp.readyState == 4) //请求状态为4表示成功
{
if(xmlHttp.status==200) //表示返回状态是成功的
{
var response = xmlHttp.responseText;
test1.innerHTML=response;
//window.alert(xmlHttp.responseText);
}
}
}
</script>
checkname.aspx.cs 中的代码:

protected void Page_Load(object sender, EventArgs e)
{
int flag=0;
SqlConnection conn = DB.createconnection();
string username=Request["name"];
string sql = "select count(*) from Users where UserName='" + username + "'";
SqlCommand myCommand = new SqlCommand(sql,conn);
myCommand.Connection.Open();
try
{
flag=(int)myCommand.ExecuteScalar();
}
catch(Exception ex)
{
throw new Exception(ex.ToString());
}
finally
{
myCommand.Connection.Close();
}
if (flag == 1)
{
Response.Write("非常抱歉!!该用户名已经存在!!");

}
else
{
Response.Write("恭喜您!!该用户名可以注册!!");

}
}

这样怎么实现不了?还有xmlHttp.responseText 如何获得获得验证后的值的?Response.Write这样就可以了吗?谢谢...
...全文
178 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复

谢谢楼上的...加上Response.End();这个就可以了。但不知道为什么这样哦..
Go 旅城通票 2007-11-29
  • 打赏
  • 举报
回复
使用ajax的那个页面的代码贴出来.

checkname.aspx.cs 中的代码最后加一句

if   (flag   ==   1) 
{
Response.Write("非常抱歉!!该用户名已经存在!!");

}
else
{
Response.Write("恭喜您!!该用户名可以注册!!");
}
////////
Response.End();
Go 旅城通票 2007-11-29
  • 打赏
  • 举报
回复
Response.End();

是停止向客户端继续输出代码,我想你的这个aspx页面中应该还有其他的html代码,没加上
Response.End();的话会把这些html代码一起发送到客户端
  • 打赏
  • 举报
回复

好象就只上面那点代码列......我觉得应该是checkname.aspx.cs代码中的问题..Response.Write就能把值传过去吗?
Go 旅城通票 2007-11-28
  • 打赏
  • 举报
回复
就你的js代码来看没什么问题,贴出其他的代码来看看
  • 打赏
  • 举报
回复

好象这个xmlHttp.responseText 没有获得值.....怎么解决啊??急急啊..
  • 打赏
  • 举报
回复

点检测按钮时,就出现loading.... 然后页面状态栏上显示网页代码有错误..
lkh42002 2007-11-27
  • 打赏
  • 举报
回复
查看源代码
  • 打赏
  • 举报
回复

访问了,输出这句 : 恭喜您!!该用户名可以注册!!
lkh42002 2007-11-27
  • 打赏
  • 举报
回复
checkname.aspx?name=fdsafda
你在浏览器里访问一下看看,可能有其他不需要的代码生成。

52,797

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧