General handler instance in. net,. net program instance
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:
/// <Span style = "font-family: Arial, Helvetica, sans-serif;"> database queries are not involved, which is relatively simple </span>
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
<Form action = "BackLogin. ashx "method =" post "> <input type =" text "name =" username "value =" {num} "/> <input type =" text "name =" password "/> <input type =" submit "value =" login "/> </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:
5. Reflection
The first implementation method is not asynchronous and does not work well. ajax Asynchronization is more underlying than Post operations.
6. easy to make mistakes
1. the name value in the <input> label is inconsistent with the name obtained from the form in the general processing program.
2. Various {} () matches are inaccurate, resulting in imperceptible syntax errors
7. Summary
Make sure that you have a good idea before programming. Under the guidance of your ideas, be careful.