完整的 dataType=text/plain jquery ajax 登陸驗證

Html:javascript

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <meta charset="utf-8" />
 5     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 6     <title>校園通銷售管理系統-用戶登陸</title>
 7     <link href="/css/base.css" rel="stylesheet" type="text/css" />
 8     <link href="/css/login.css" rel="stylesheet" type="text/css" />
 9     <script src="/js/jquery.js" type="text/javascript"></script>
10     <script src="/js/jquery.md5.js" type="text/javascript"></script>
11     <script src="/js/login.js" type="text/javascript"></script>
12 </head>
13 <body>
14     <div class="login_main">
15         <div class="login">
16             <span class="s1">
17                 <label id="errMsg"></label>
18             </span>
19             <ul>
20                 <li>
21                     <span class="s2">用戶名:</span>
22                     <span class="s3">
23                         <input type="text" id="txtUserName" value="" />
24                     </span>
25                 </li>
26                 <li>
27                     <span class="s2">密 碼:</span>
28                     <span class="s3">
29                         <input type="password" id="txtUserPwd" value="" />
30                     </span>
31                     <span class="s4">
32                         <input type="button" id="btnLogin" value="登陸" />
33                     </span>
34                 </li>
35             </ul>
36         </div>
37     </div>
38 </body>
39 </html>
Html或者Aspx

js:css

 1 $(document).ready(function () {
 2     $("#btnLogin").click(function () {
 3         var name = $.trim($("#txtUserName").val());
 4         var password = $.trim($("#txtUserPwd").val());
 5         if (name == "" || name == null) {
 6             $("#errMsg").text("用戶名不能爲空!");
 7         } else if (password == "" || password == null) {
 8             $("#errMsg").text("密碼不能爲空!");
 9         } else {
10 
11             $.ajax({
12                 type: "post",
13                 url: "/url/login.ashx",
14                 data: {
15                     name: encodeURI(name, "utf-8"),
16                     password: $.md5(password)
17                 },
18                 dataType: "text/plain",
19                 success: function (data) {
20                     $("#errMsg").text(data);
21                 }
22             });
23         }
24     });
25 });
Js

Ashxhtml

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Security;
 6 using System.Runtime.Serialization;
 7 using Newtonsoft.Json;
 8 using Newtonsoft.Json.Converters;
 9 
10 namespace Sales.Web.url
11 {
12     /// <summary>
13     /// LoginHandler 的摘要說明
14     /// </summary>
15     public class LoginHandler : IHttpHandler
16     {
17         public void ProcessRequest(HttpContext context)
18         {
19             context.Response.ContentType = "text/plain";
20             try
21             {
22                 if (HttpUtility.UrlDecode(context.Request["name"] ?? "") == "用戶名" && (context.Request["password"] ?? "").ToLower() == FormsAuthentication.HashPasswordForStoringInConfigFile("123456", "MD5").ToLower())
23                 {
24                     context.Response.Write("true");
25                 }
26                 else
27                 {
28                     context.Response.Write("false");
29                 }
30             }
31             catch (Exception ex)
32             {
33                 context.Response.Write(ex.Message);
34             }
35             finally
36             {
37                 context.Response.End();
38             }
39         }
40 
41         public bool IsReusable
42         {
43             get
44             {
45                 return false;
46             }
47         }
48     }
49 }
Ashx
相關文章
相關標籤/搜索