Ajax判断用户是否存在(post方法)

本文介绍了一种使用XMLHttpRequest进行异步请求的方法,通过在Register.aspx页面上触发按钮点击事件来发送POST请求到UserIsExist.aspx页面,以验证用户名是否已被注册。验证结果将返回并显示在Register.aspx页面上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、以下是Register.aspx页面的代码,主要是通过Xmlhttp来向UserIsExist.aspx页面Post要检查的用户名过去。     

<form id=Form1 method=post runat="server">
    
<input id=aa  size=66> <input onclick="check();" type="button" value="查看用户是否存在"></form>
<script language="javascript">
<!--
function check()
{
var UserName=document.getElementById("txtName").value;

var user="username="+escape(UserName);
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
var xmldoc = new ActiveXObject("Msxml.DOMDocument");
xmlhttp.Open("POST","UserIsExist.aspx",false);
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=GB2312");
xmlhttp.Send(user);
xmlhttp.onreadystatechange=function() {

if (xmlhttp.readyState==4) {
alert(xmlhttp.responseText)
}
}

if(xmlhttp.status!=200)
{
alert ('网络故障(xmlhttp.status='+xmlhttp.status+'),请稍后再试!');
}
else
{
document.getElementById('message').innerHTML = xmlhttp.responseText;
}
}
//-->
</script>2、在UserIsExist.aspx的Page_Load中加入下面代码

  string username=Server.UrlDecode(Request.Form["username"].ToString());
    if(User.IsExist(username)==true)
    {
     Response.Write("<font color=red>对不起,你的用户名已被注册过了.</font>");
    }
    else
    {
     Response.Write("<font color=red>恭喜您,此用户名还未被使用,你可以继续使用!</font>");
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值