標籤:style blog http io ar os sp java for
<1>
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %><head runat="server"> <title>ajax請求</title> <link type="text/css" rel="stylesheet" href="/Content/style.css" /> <script type="text/javascript" src="/Scripts/jquery-1.8.3.min.js"></script> <script type="text/javascript" src="/Scripts/js.js"></script></head><body> <!--頂部+logo+導航--> <div class="logo_box"> <div id="logo"> <a title="ajax請求">ajax請求</a></div> </div> <!----> <div class="loginCon"> <div class="loginBanner"> <img src="/Images/4499633_182932517000_2.jpg" /></div> <div class="loginBox"> <h2> <span class="fl">會員登入</span><span class="newUser">沒有帳號?<a href='<%=Url.Action("Register","Account") %>'>立即註冊</a></span></h2> <form id="formData"> <div class="loginForm"> <div class="inputBox"> <input type="text" name="user" value="使用者名稱/手機號" class="userId" /> </div> <div class="inputBox"> <input type="text" value="密碼" class="textStyle" /> <input type="password" name="pwd" class="passwordStyle none" /> </div> <div class="warn">使用者名稱或密碼錯誤!</div> <div class="remember"> <label> <input type="checkbox" name="remembered" checked /> 自動登入</label> <a class="forget" href='<%=Url.Action("ResetPwd","Login") %>' >忘記密碼?</a> </div> <input class="loginBtn" type="button" value="登入"/> </div> </form> </div> </div></body><script type="text/javascript"> $(function () { $('.userId,.passwordStyle').on('keyup', function (e) { if (e.keyCode == 13) { $('.loginBtn').trigger('click'); } }); $('.loginBtn').on('click', function () { $(".warn").hide(); var pwd = $('.passwordStyle').val(); if (pwd == '') { $(".warn").show().html('請輸入密碼'); return false; } var data = $("#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>
<2>
控制器
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using System.Text;namespace bigtree.Controllers{ using bigtree.Models; using bigtree.Model; using bigtree.lib; using System.Net.Mail; using System.Text.RegularExpressions; public class LoginController : Controller { public ActionResult Index() { return View(); } /// <summary> /// 檢查登陸 /// </summary> /// <param name="f"></param> /// <returns></returns> [HttpPost] public ActionResult CheckLoginInfo(FormCollection f) { try { //post: user , pwd ,remembered string user = f["user"].Trim(); string pwd = f["pwd"].Trim(); string remembered = f["remembered"].Trim(); JsonResult res = new JsonResult(); if (string.IsNullOrEmpty(user) || string.IsNullOrEmpty(pwd)) { res.Data = new { status = 0 }; } //MD5加密後的密碼 pwd = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(pwd, "md5").ToLower(); //從資料庫讀取 Common.WebUser account = 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") { HttpCookie cookie = new HttpCookie("LoginInfo", account.Id.ToString()); //3天有效 cookie.Expires.AddDays(3); Response.Cookies.Add(cookie); } else { HttpCookie cookie = new HttpCookie(account.Id.ToString(), account.Id.ToString()); //使失效 cookie.Expires.AddYears(-1); Response.Cookies.Add(cookie); } } return res; } catch (Exception ex) { throw ex.InnerException; } } }}
Ajax請求。資料的提交,參數的傳遞。FormCollection