简单ajax注册

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>
		login
	</title>
	<script type="text/javascript" src="send.js"></script>
</head>
<body>
	<div>
		<input type="text" name="UserName" id="UserName" onblur="usercheck()"> <span id="showerror"></span>
	</div>	

	<input type="password" name="Pwd" id="Pwd">
	<button id="login" >登陆</button>



</body>
</html>

此代码是html一个简单的登录界面

var http_request=false;

function usercheck()
{
	var username=document.getElementById("UserName").value;
	if(username=="")
	{
		var showerror=document.getElementById("showerror");
		showerror.innerText="用户名不能为空!";

	}
	else
	{
		send_request("server.aspx?username="+username+"&r="+Math.random());

	}



}



function send_request(url)
{
	http_request=false;

	if(window.XMLHttpRequest)
	{
		http_request=new XMLHttpRequest();
		if(http_request.overrideMimeType)
		{

			http_request.overrideMimeType('text/xml');

		}
	}
	else 
		if(window.ActiveXObject)
		{
			try
			{
				http_request=new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e)
			{
				try
				{
					http_request=new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch(e)
				{

				}

			}


		}

	if(!http_request)
	{
		window.alert("错误!");
		return false;

	}


	http_request.onreadystatechange=processRequest;
	http_request.open("GET",url,true);
	http_request.send(null);


}


function processRequest()
{
	if(http_request.readyState==4)
	{
		if(http_request.status==200)
		{
			var str=http_request.responseText;
			showMsg(str);

		}
		else
		{

			alert("有错误!");

		}


	}

}


function showMsg(str)
{
	if(str=="1")
	{
		document.getElementById("showerror").innerText="";

	}
	else
		if(str=="0")
		{

			document.getElementById("showerror").innerText="用户名存在!";

		}
		else 
			if(str=="2")
			{
				document.getElementById("showerror").innerText="用户名不能为空!";
			}

}

此是send.js  里面包含了对用户名的验证

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;

public partial class server : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string username = Request.QueryString["username"].ToString();
        if (username == "")
        {
            Response.Write("2");
            return;
        }
        else
            if (username == "cxw")
        {
            Response.Write("0");
            
            return;
        }
        else
        {
            Response.Write("1");
            return;
        }

    }
}

此是.aspx后缀的服务器后台,用此来接受客户端的数据,从而返回数据

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值