.NET的Ajax请求数据提交实例
本文实例讲述了.NET的Ajax请求数据提交实现方法。分享给大家供大家参考。具体如下:
<%@PageLanguage="C#"Inherits="System.Web.Mvc.ViewPage<dynamic>"%> <headrunat="server"> <title>ajax请求</title> <linktype="text/css"rel="stylesheet"href="/Content/style.css"/> <scripttype="text/javascript"src="/Scripts/jquery-1.8.3.min.js"></script> <scripttype="text/javascript"src="/Scripts/js.js"></script> </head> <body> <!--顶部+logo+导航--> <divclass="logo_box"> <divid="logo"> <atitle="ajax请求">ajax请求</a></div> </div> <!----> <divclass="loginCon"> <divclass="loginBanner"> <imgsrc="/Images/4499633_182932517000_2.jpg"/></div> <divclass="loginBox"> <h2> <spanclass="fl">会员登录</span><spanclass="newUser">没有账号?<ahref='<%=Url.Action("Register","Account")%>'>立即注册</a></span></h2> <formid="formData"> <divclass="loginForm"> <divclass="inputBox"> <inputtype="text"name="user"value="用户名/手机号"class="userId"/> </div> <divclass="inputBox"> <inputtype="text"value="密码"class="textStyle"/> <inputtype="password"name="pwd"class="passwordStylenone"/> </div> <divclass="warn">用户名或密码错误!</div> <divclass="remember"> <label> <inputtype="checkbox"name="remembered"checked/> 自动登录</label> <aclass="forget"href='<%=Url.Action("ResetPwd","Login")%>'>忘记密码?</a> </div> <inputclass="loginBtn"type="button"value="登录"/> </div> </form> </div> </div> </body> <scripttype="text/javascript"> $(function(){ $('.userId,.passwordStyle').on('keyup',function(e){ if(e.keyCode==13){ $('.loginBtn').trigger('click'); } }); $('.loginBtn').on('click',function(){ $(".warn").hide(); varpwd=$('.passwordStyle').val(); if(pwd==''){ $(".warn").show().html('请输入密码'); returnfalse; } vardata=$("#formData").serialize(); $.post("/login/checkLoginInfo",data,function(ajaxObj){ //回传内容{status:1(success)/0(fail),} if(ajaxObj.status==0||status==null){ $(".warn").show().html('用户名或密码错误!'); }else{ //登陆成功,跳转都制定页面 window.location='/memberCenter/index'; } },"json"); }); }); </script> </html>
控制器
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Web; usingSystem.Web.Mvc; usingSystem.Text; namespacebigtree.Controllers { usingbigtree.Models; usingbigtree.Model; usingbigtree.lib; usingSystem.Net.Mail; usingSystem.Text.RegularExpressions; publicclassLoginController:Controller { publicActionResultIndex() { returnView(); } ///<summary> ///检查登陆 ///</summary> ///<paramname="f"></param> ///<returns></returns> [HttpPost] publicActionResultCheckLoginInfo(FormCollectionf) { try { //post: user,pwd,remembered stringuser=f["user"].Trim(); stringpwd=f["pwd"].Trim(); stringremembered=f["remembered"].Trim(); JsonResultres=newJsonResult(); if(string.IsNullOrEmpty(user)||string.IsNullOrEmpty(pwd)) { res.Data=new{status=0}; } //MD5加密后的密码 pwd=System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(pwd,"md5").ToLower(); //从数据库读取 Common.WebUseraccount=MemberInfoService.GetMemberIdForCheck(user,pwd); if(account==null) { res.Data=new{status=0}; } else { //{status:1(success)/0(fail),} res.Data=new{status=1}; //todo:登陆成功,记录登陆用户信息保存登陆状态 FunSession.SetSession(account); //是否记住登录 if(remembered=="on") { HttpCookiecookie=newHttpCookie("LoginInfo",account.Id.ToString()); //3天有效 cookie.Expires.AddDays(3); Response.Cookies.Add(cookie); } else { HttpCookiecookie=newHttpCookie(account.Id.ToString(),account.Id.ToString()); //使失效 cookie.Expires.AddYears(-1); Response.Cookies.Add(cookie); } } returnres; } catch(Exceptionex) { throwex.InnerException; } } } }
希望本文所述对大家的.NET程序设计有所帮助。