Jquery Ajax 登录,服务端分别为 aspx,ashx,asmx
??? 原文: http://www.cnblogs.com/StudyLife/archive/2012/02/22/2363174.html
??? 【理解asp.net中的ashx、asmx】
常規(guī)的Jquery Ajax 驗證登錄,主要有3種服務(wù)端頁面相應(yīng) ,也就是 aspx,ashx,asmx即webserivice 。
?
下面分別用3種方式來(aspx,ashx,asmx)做服務(wù)端來處理 Jquery? Ajax 傳過來的用戶名和密碼驗證!
?
例: Login.html 請求用戶名和密碼驗證!
<head> <script type="text/javascript">$(document).ready(function() {$("#go").bind('click', function() {var name = $.trim($("#txtName").val()); // $.trim()去除空格var pwd = $.trim($("#txtPwd").val());if (name == "") { $("#nameResult").html("<font color='red'>用戶名不能為空</font>"); }else { $("#nameResult").html(""); }if (pwd == "") { $("#pwdResult").html("<font color='red'>密碼不能為空</font>"); }else { $("#pwdResult").html(""); }if (name != "" && pwd != "") {//post 請求$.ajax({type: "post",//服務(wù)器相應(yīng)ajax請求,可以有 aspx頁面,ashx頁面 url: "AjaxLogin.aspx","AjaxLoginAshx.ashx",url: "AjaxLoginAsmx.asmx/login", //調(diào)用 webserivice 下的login方法cache: false,data: "name=a&&pwd=b",//aspx頁面為服務(wù)器相應(yīng)success: function(html) { location.href = "default.aspx"; $("#go").attr("disabled", false) },error: function(html) { alert("登錄失敗"); $("#go").attr("disabled", false) }});}});});</script> </head><body><form id="form1" runat="server"><div>用戶名<asp:TextBox ID="txtName" runat="server" ></asp:TextBox><span id="nameResult"></span><br />密碼<asp:TextBox ID="txtPwd" runat="server"></asp:TextBox><span id="pwdResult"></span><br /><br /><input type="button" id="go" value="登錄" /><asp:Button ID="go1" runat="server" Text="服務(wù)器控件登錄" onclick="go_Click" /></div></form> </body>
?
?
?1.服務(wù)端為 AjaxLogin.aspx 頁面
首先修改 $.ajax 的 Url:"AjaxLogin.aspx"
?
protected void Page_Load(object sender, EventArgs e){if (Request.Form["name"] != null && Request.Form["pwd"] != null){if (Request.Form["name"].ToString().Trim() != "" && Request.Form["pwd"].ToString() != ""){string name = Request.Form["name"].ToString().Trim();string pwd = Request.Form["pwd"].ToString();Response.Cookies.Clear();Response.Cookies.Add(new HttpCookie("name", name)); //添加cookieResponse.Cookies.Add(new HttpCookie("pwd",pwd));//添加密碼Response.Write("你已經(jīng)成功登錄");}}}?
?2. 服務(wù)端為 AjaxLoginAshx.ashx 頁面? (一般處理程序)
首先修改 $.ajax 的 Url:"AjaxLoginAshx.ashx"; 另:ashx以context.Response.Write 返回內(nèi)容
?
[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]public class AjaxLoginAshx : IHttpHandler{public void ProcessRequest(HttpContext context){//context.Response.ContentType = "text/plain";//context.Response.Write("Hello World");if (context.Request.Form["name"] != null && context.Request.Form["pwd"] != null){if (context.Request.Form["name"].ToString().Trim() != "" && context.Request.Form["pwd"].ToString() != ""){string name = context.Request.Form["name"].ToString().Trim();string pwd = context.Request.Form["pwd"].ToString();context.Response.Cookies.Clear();context.Response.Cookies.Add(new HttpCookie("name", name));context.Response.Cookies.Add(new HttpCookie("pwd", pwd));context.Response.Write("你已經(jīng)成功登錄");}else{context.Response.Write("沒成功登錄1");}}else{context.Response.Write("沒成功登錄2");}}public bool IsReusable{get{return false;}}?
3. 服務(wù)端為 AjaxLoginAsmx.asmx頁面? (SOAP方式HTTP訪問,用XML返回)
?? 首先修改 $.ajax 的 Url:"AjaxLoginAsmx.asmx/login";?? 注意:? / 后面是方法名
另asmx頁面以return 返回內(nèi)容,response.cookies.add添加cookie
/// <summary>/// AjaxLoginAsmx 的摘要說明/// </summary>[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)][System.ComponentModel.ToolboxItem(false)]public class AjaxLoginAsmx : System.Web.Services.WebService{[WebMethod]public string HelloWorld(){return "Hello World";}[WebMethod]public string login(){if (HttpContext.Current.Request.Form["name"] != null && HttpContext.Current.Request.Form["pwd"] != null){if (HttpContext.Current.Request.Form["name"].ToString().Trim() != "" && HttpContext.Current.Request.Form["pwd"].ToString() != ""){string name = HttpContext.Current.Request.Form["name"].ToString().Trim();string pwd = HttpContext.Current.Request.Form["pwd"].ToString();//HttpContext.Current.Response.Cookies.Add(new HttpCookie("name", name));//HttpContext.Current.Response.Cookies.Add(new HttpCookie("pwd", pwd));//HttpContext.Current.Response.Write("你已經(jīng)成功登錄");HttpContext.Current.Response.Cookies.Add(new HttpCookie("a", "123"));return "Hello World";}else{return "error1";}}else{return "error2";}}分類: Jquery & Ajax&Javascript
總結(jié)
以上是生活随笔為你收集整理的Jquery Ajax 登录,服务端分别为 aspx,ashx,asmx的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 东方甄选1752万元扩建河南焦作自营品工
- 下一篇: aspx、ashx、asmx文件处理请求