asp.net AJAX 验证用户名是否存在 -Jquery

来源:互联网 发布:scrivener mac破解版 编辑:程序博客网 时间:2024/05/19 07:43

 

异步刷新实现方式有多种,也可以借助JS的多种框架,下面是使用JQuery框架实现的AJAX 验证用户名是否存在
jQuery.ajax概述
HTTP 请求加载远程数据。

 

通过jQuery 底层 AJAX 实现。简单易用的高层实现见 $.get, $.post 等。$.ajax() 返回其创建的 XMLHttpRequest 对象。大多数情况下你无需直接操作该对象,但特殊情况下可用于手动终止请求。

$.ajax() 只有一个参数:参数 key/value 对象,包含各配置及回调函数信息。详细参数选项见下。

注意: 如果你指定了 dataType 选项,请确保服务器返回正确的 MIME 信息,(如 xml 返回 "text/xml")。错误的 MIME 类型可能导致不可预知的错误。见 Specifying the Data Type for AJAX Requests 。

注意:如果dataType设置为"script",那么在远程请求时(不在同一个域下),所有POST请求都将转为GET请求。(因为将使用DOM的script标签来加载)

jQuery 1.2 中,您可以跨域加载 JSON 数据,使用时需将数据类型设置为 JSONP。使用 JSONP 形式调用函数时,如 "myurl?callback=?" jQuery 将自动替换 ? 为正确的函数名,以执行回调函数。数据类型设置为 "jsonp" 时,jQuery 将自动调用回调函数。

 

1、请求页面AJax.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Register.aspx.cs" Inherits="register" %>

<!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">
    <link href="css/public.css" rel="stylesheet" type="text/css" />
    <title>用户注册</title>
    <script src="Scripts/jquery-1.3.2.js" type="text/javascript"></script>
    <script type="text/javascript">
        function JudgeUserName() {
            $.ajax({
                type: "GET",
                url: "AjaxUserInfo.aspx",
                dataType: "html",
                data: "userName=" + $("#u_name").val(),
                beforeSend: function (XMLHttpRequest) {
                    $("#showResult").text("正在查询...");
                    //Pause(this,100000);
                },
                success: function (msg) {
                    $("#showResult").html(msg);
                    $("#showResult").css("color", "red");
                },
                complete: function (XMLHttpRequest, textStatus) {
                    //隐藏正在查询图片
                },
                error: function () {
                    //错误处理
                }
            });
        }

     </script>

</head>
<body>
<form id="form1" runat="server">
             <asp:TextBox ID="u_name" runat="server" maxlength='30' onblur="JudgeUserName();" ></asp:TextBox>
             <div id="showResult"></div>  </form>
</body>
</html>

 

2 、页面AjaxUserInfoModify.aspx

后台代码

 protected void Page_Load(object sender, EventArgs e)
    {
        string userName 
= Request.QueryString["userName"].ToString ();
        
if (userName == "James Hao")
        {
            Response.Write (
"用户名已经存在!");
        }
        
else
        {
            Response.Write (
"您可以使用此用户名!");
        }
}

 

 


 

原创粉丝点击