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.