.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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值