. Net, jquery, ajax, and wcf enable local refresh of Database User Name detection, jquerywcf

Source: Internet
Author: User

. Net, jquery, ajax, and wcf enable local refresh of Database User Name detection, jquerywcf

Jquery code

$ (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 ("this user name does not exist! "); $ (" # Showd "). val (" 1 ");} else if (msg =" have "){
$ ("# Usernamee"). attr ("class", "controls ");
$ ("# Usernamecheck" pai.html ("");
$ ("# Showd"). val ("0 ");
}}});}});});

This jquery event is triggered when the textbox with id user_name loses focus. The user_name value is post to Handler. ashx and different situations are determined based on the value returned by Handler. ashx.

 

Handler. ashx code
    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;        }

This code is used to access the wcf Service hosted in iis. It checks whether the input user name exists and sends the user name to Handler through ajax post. handler. ashx references the WCF Service WS and instantiates a WS. the new IserviceClient object client calls the SqlCmd method in the wcf Service. If the user name does not exist, the bootstrap error style is displayed. If the user name does not exist, it is not processed if it is correct.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.