General handler instance in. net

Source: Internet
Author: User

General handler instance in. net

Recently, I learned some asynchronous jQuery operations in the general processing program, so I tried to make a small login and exercise myself.

1. First, a new project LoginDemo is created, and a general handler BackLogin. ashx is added. The specific code is as follows:

/// The database query is not involved. It is relatively simple to write here
Public class BackLogin: IHttpHandler {public void ProcessRequest (HttpContext context) {context. response. contentType = "text/plain"; string username = context. request. form ["username"]; string password = context. request. form ["password"]; if (username = "" | password = "") {context. response. write ("Enter the user name or password! ");} Else {if (username =" 001 "& password =" 001 ") {context. response. write ("success");} else {context. response. write ("the user name or password is incorrect. Please log on again! ") ;}} Context. Response. End () ;}public bool IsReusable {get {return false ;}}}
2. Add a Web page Login. aspx as the Login page. The browser is as follows:

3. Three implementation methods

1. Form

    
2. asynchronous Post

<Script type = "text/javascript"> $ (function () {$ ("# Login "). click (function () {$. post ("BackLogin. ashx ", {" username ": $ (" # username "). val (), "password": $ ("# password "). val ()}, function (msg) {if (msg = "success") {alert ("Login successful ");} else if (msg = "the user name or password is incorrect. Please log on again! ") {Alert (" Logon Failed! ");} Else {alert (" Enter your username and password! ") ;}}) ;}); // $ (" Form1 "). submit () ;}) </script>

3. asynchronous ajax

<Script type = "text/javascript"> $ (document ). ready (function () {$ ("# Login "). click (function () {$. ajax ({url: "BackLogin. ashx ", type:" POST ", data:" username = "+ escape ($ ('# username '). val () + "& password =" + escape ($ ('# password '). val (), dataType: "text/plain", async: "true", success: function (msg) {var data = eval ("(" + msg. d + ")"); if (data = "success") {alert ("Login successful! ");} Else {alert (" Logon Failed! ") ;}}) ;}})}); </Script>

4. Running Effect

The browser displays the following information when entering the user name and password:

Make sure that you have a good idea before programming. Under the guidance of your ideas, be careful.

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.