XMLHttpRequest实现无刷新验证用户名
在用户注册时,我们经常需要检查用户名是否存在,本文就是实现无刷新验证用户名
打开开发环境VS 2005,新建项目(或打开现有项目),新建一个Web窗体,命名为 Default.aspx
代码如下:
view plaincopy to clipboardprint?
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 runat="server">
<title>无标题页</title>
<script type="text/javascript"><!--
var xmlHttp=null;
function createXMLHttpRequest()
{
if(xmlHttp == null){
if(window.XMLHttpRequest) {
//Mozilla 浏览器
xmlHttp = new XMLHttpRequest();
}else if(window.ActiveXObject) {
// IE浏览器
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
//alert('创建失败');
}
}
}
}
}
function openAjax()
{
if( xmlHttp == null)
{
createXMLHttpRequest();
if( xmlHttp == null)
{
//alert('出错');
return ;
}
}
var val=document.getElementById('txt').value;
xmlHttp.open("get","VerifyUserNameHandler.ashx?para="+val+"&date="+new Date(),true);
xmlHttp.onreadystatechange=xmlHttpChange;
xmlHttp.send(null);
document.getElementById('resultSpan').innerText='正在检查,请稍候...';
}
function xmlHttpChange()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
var res=xmlHttp.responseText;
document.getElementById('resultSpan').innerText=res;
if(res=='恭喜,用户名可以使用。')
{
setTimeout("document.getElementById('resultSpan').innerText='';",2000);
}
else if(res=='抱歉,用户名已被使用。')
{
document.getElementById('txt').focus();
}
}
}
}
// --></script>
</head>
<body>
<form id="form1" runat="server">
用户名:<input type="text" id='txt' value="Sandy" onblur="openAjax();" /> <span id="resultSpan"></span>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 runat="server">
<title>无标题页</title>
<script type="text/javascript"><!--
var xmlHttp=null;
function createXMLHttpRequest()
{
if(xmlHttp == null){
if(window.XMLHttpRequest) {
//Mozilla 浏览器
xmlHttp = new XMLHttpRequest();
}else if(window.ActiveXObject) {
// IE浏览器
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
//alert('创建失败');
}
}
}
}
}
function openAjax()
{
if( xmlHttp == null)
{
createXMLHttpRequest();
if( xmlHttp == null)
{
//alert('出错');
return ;
}
}
var val=document.getElementById('txt').value;
xmlHttp.open("get","VerifyUserNameHandler.ashx?para="+val+"&date="+new Date(),true);
xmlHttp.onreadystatechange=xmlHttpChange;
xmlHttp.send(null);
document.getElementById('resultSpan').innerText='正在检查,请稍候...';
}
function xmlHttpChange()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
var res=xmlHttp.responseText;
document.getElementById('resultSpan').innerText=res;
if(res=='恭喜,用户名可以使用。')
{
setTimeout("document.getElementById('resultSpan').innerText='';",2000);
}
else if(res=='抱歉,用户名已被使用。')
{
document.getElementById('txt').focus();
}
}
}
}
// --></script>
</head>
<body>
<form id="form1" runat="server">
用户名:<input type="text" id='txt' value="Sandy" onblur="openAjax();" /> <span id="resultSpan"></span>
</form>
</body>
</html>
然后新建一个一般处理程序,命名为 VerifyUserNameHandler.ashx
代码如下:
view plaincopy to clipboardprint?
<%@ WebHandler Language="C#" Class="VerifyUserNameHandler" %>
using System;
using System.Web;
using System.Collections;
using System.Collections.Generic;
public class VerifyUserNameHandler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
//context.Response.ContentType = "text/plain";
string _name = context.Request.QueryString["para"];
_name = string.IsNullOrEmpty(_name) ? "" : _name;
System.Threading.Thread.Sleep(3000);//用线程来模拟数据库查询工作
string[] Names = new string[] { "Sandy", "阿非", "abc" };//这里用Names数组来代替数据库中的结果集
if (Array.IndexOf<string>(Names, _name) == -1)
{
context.Response.Write("恭喜,用户名可以使用。");
}
else
{
context.Response.Write("抱歉,用户名已被使用。");
}
}
public bool IsReusable {
get {
return false;
}
}
}
<%@ WebHandler Language="C#" Class="VerifyUserNameHandler" %>
using System;
using System.Web;
using System.Collections;
using System.Collections.Generic;
public class VerifyUserNameHandler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
//context.Response.ContentType = "text/plain";
string _name = context.Request.QueryString["para"];
_name = string.IsNullOrEmpty(_name) ? "" : _name;
System.Threading.Thread.Sleep(3000);//用线程来模拟数据库查询工作
string[] Names = new string[] { "Sandy", "阿非", "abc" };//这里用Names数组来代替数据库中的结果集
if (Array.IndexOf<string>(Names, _name) == -1)
{
context.Response.Write("恭喜,用户名可以使用。");
}
else
{
context.Response.Write("抱歉,用户名已被使用。");
}
}
public bool IsReusable {
get {
return false;
}
}
}
到这里程序已经完成。
主要是利用了XMLHttpRequest对象采用异步的方式去访问服务器,获得响应后触发定义好的回调函数
本文是XMLHttpRequest对象异步方式对服务器发送Get方式的请求,访问服务器的文件为.ashx
本文开发环境为 VS 2005
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述