.net、jquery、ajax、wcf实现数据库用户名检测局部刷新

jquery代码

$(function() {  
    $("#user_name").blur(function(){  
var user_name=$("#user_name").val();
if(user_name!="")
{
        $.ajax({  
               type: "POST",  
               url:"Handler.ashx",  
               data:{name:user_name},  
               beforeSend:function(){  
               
               },  
               success: function(msg){  
                if(msg=="nohave"){$("#usernamee").attr("class","form-group has-error");
                                 $("#usernamecheck").html("不存在此用户名!");
                                 $("#showd").val("1");
                                 } 
                else if(msg=="have"){
$("#usernamee").attr("class","controls");
$("#usernamecheck").html("");
$("#showd").val("0");
} } }); } }); });

  当id为user_name的textbox失去焦点时触发此jquery事件,将user_name的值post到Handler.ashx中,根据Handler.ashx返回的值判断不同的情况。

Handler.ashx代码
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        if (context.Request["name"].ToString()!="")
        {
            string username = context.Request["name"].ToString();
            WS.IserviceClient client = new WS.IserviceClient();
            string sqlstr = "select *  from SystemUser where username='" + username + "'";
            int k = client.SqlCmd(sqlstr);
            if(k>0)
            {
                context.Response.Write("have");
            }
            else
            {
                context.Response.Write("nohave");
            }
        }
    }

    public bool IsReusable {
        get {
            return false;
        }

  此段代码访问托管在iis里的wcf服务,检测输入的用户名是否存在,通过ajax post用户名到Handler.ashx里,Handler.ashx引用了WCF服务WS,实例化一个WS.IserviceClient新对象client,client调用wcf服务里的SqlCmd方法,如果用户名不存在则显示bootstrap错误样式,正确则不处理。

原文地址:https://www.cnblogs.com/ssvip/p/6796439.html