最近在學習一般處理程序,也學習了一些jQuery的異步操作,于是就想著親手做一個小的登陸,鍛煉一下自己。
1、首先新建了一個項目LoginDemo,在此基礎上又添加了一個一般處理程序BackLogin.ashx,具體代碼如下
?| 1 | ///<span style="font-family: Arial, Helvetica, sans-serif;" class="">沒有牽扯到數據庫查詢,在這寫的比較簡單</span> |
| 123456789101112131415161718192021222324252627282930313233 | 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("請填寫用戶名或密碼!");}else{if (username == "001" && password == "001") {context.Response.Write("success");}else{context.Response.Write("用戶名或密碼錯誤,請重新登錄!");}}context.Response.End();}public bool IsReusable{get{return false;}}} |
2、添加一個Web頁面Login.aspx,作為登陸頁面,瀏覽器效果圖如下所示

3、三種實現方式
1. Form表單形式
?| 12345 | <form action="BackLogin.ashx" method="post"> <input type="text" name="username" value="{num}"> <input type="text" name="password"> <input type="submit" value="登陸"> </form> |
2、異步Post方式
?| 12345678910111213141516171819 | <script type="text/javascript">$(function () {$("#Login").click(function () {$.post("BackLogin.ashx", { "username": $("#username").val(), "password": $("#password").val() },function (msg) {if (msg == "success") {alert("登陸成功");}else if (msg == "用戶名或密碼錯誤,請重新登錄!") {alert("登錄失敗!");}else {alert("請輸入用戶名和密碼!");}});});< |