之前一直在CS,今天第一次接触BS,好多东西都不太熟悉。今天用js、form表单通过一般处理程序实现的登陆,虽然简单,但通过尝试知道了其中的调用关系了。
第一种,JS调用代码:
<!--HTML代码--> <head runat="server"> <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $(‘#btn_logon‘).click(function () { var user = $("#usernameid").val(); var pass = $("#passwordid").val(); $.ajax({ type: "GET", url: "CheckPass.ashx", data: { usernamename: user, passwordname: pass }, dataType: "text", success: function (ssss) { alert(ssss) }, error: function (XMLHttpRequest, textStatus, errorThrown) { console.log(textStatus); } }); }); }); </script> <title>统一登录平台</title> <style type="text/css"> #btn_logon { height: 20px; } </style> </head> <body> <form id="Form1" class="login-form " runat="server" > <label >统一登录平台</label> <div > <label >Username</label> <div > <input type="text" autocomplete="off" placeholder="用户名" id="usernameid" runat="server" /> </div> </div> <div > <label >Password</label> <div> <input type="password" autocomplete="off" placeholder="密码" id="passwordid" runat="server"/> </div> </div> <div> <button type="button" id="btn_login">登录</button> </div> </form> </body> /// <summary> /// CheckPass 的摘要说明 /// </summary> public class CheckPass : IHttpHandler { public void ProcessRequest(HttpContext context) { try { //用ajax无法跳转其他界面 context.Response.ContentType = "text/plain"; string username = context.Request.QueryString["usernamename"].ToString(); string pass = context.Request.QueryString["passwordname"].ToString(); if (username == "1" && pass == "1") { context.Response.Write("登陆成功"); } else { context.Response.Write("登陆失败"); } } catch (Exception) { throw; } } public bool IsReusable { get { return false; } } }
注意:1、ajax中的data内的参数名可以自己任意定义;2、form表单不能有action,button按钮不能定义为submit的;3、QueryString[""]中填的是ajax中data里的参数名;4、不能跳转到百度、新浪等其他网站;
第二种,form表单直接提交:
<form id="Form1" class="login-form " runat="server" action="CheckPass.ashx" > <label >统一登录平台</label> <div > <label >Username</label> <div > <input type="text" autocomplete="off" placeholder="用户名" id="usernameid" runat="server" /> </div> </div> <div > <label >Password</label> <div> <input type="password" autocomplete="off" placeholder="密码" id="passwordid" runat="server"/> </div> </div> <div> <button type="submit" id="btn_login">登录</button> </div> </form> //CheckPass.ashx public void ProcessRequest(HttpContext context) { try { //用action和submit按钮 跳转其他界面 context.Response.ContentType = "text/plain"; string username = context.Request.Form["usernameid"].ToString(); string pass = context.Request.Form["passwordid"].ToString(); if (username == "1" && pass == "1") { context.Response.Redirect("http://www.baidu.com"); } else { context.Response.Redirect("http://www.hao123.com"); } } catch (Exception) { throw; }
注意:1、action中填的是要获取数据的一般处理程序 ;2、button按钮要设成submit类型的;3、CheckPass.ashx中的Form[]里填控件的id;4、这种方式可以跳转百度、新浪的网站。
时间: 2024-10-13 00:20:13