服务器之家

服务器之家 > 正文

.NET的Ajax请求数据提交实例

时间:2019-12-14 11:29     来源/作者:shichen2014

本文实例讲述了.NETAjax请求数据提交实现方法。分享给大家供大家参考。具体如下:

  1. <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>   
  2.    
  3. <head runat="server">   
  4.     <title>ajax请求</title>   
  5.     <link type="text/css" rel="stylesheet" href="/Content/style.css" />   
  6.     <script type="text/javascript" src="/Scripts/jquery-1.8.3.min.js"></script>   
  7.     <script type="text/javascript" src="/Scripts/js.js"></script>   
  8. </head>   
  9. <body>   
  10.     <!--顶部+logo+导航-->   
  11.     <div class="logo_box">   
  12.         <div id="logo">   
  13.             <a title="ajax请求">ajax请求</a></div>   
  14.     </div>   
  15.     <!---->   
  16.     <div class="loginCon">   
  17.         <div class="loginBanner">   
  18.             <img src="/Images/4499633_182932517000_2.jpg" /></div>   
  19.         <div class="loginBox">   
  20.             <h2>   
  21.                 <span class="fl">会员登录</span><span class="newUser">没有账号?<a href='<%=Url.Action("Register","Account") %>'>立即注册</a></span></h2>   
  22.    
  23.             <form id="formData">   
  24.             <div class="loginForm">   
  25.                 <div class="inputBox">   
  26.                     <input type="text" name="user" value="用户名/手机号" class="userId" />   
  27.                 </div>   
  28.                 <div class="inputBox">   
  29.                     <input type="text" value="密码" class="textStyle" />   
  30.                     <input type="password" name="pwd" class="passwordStyle none" />   
  31.                 </div>   
  32.                 <div class="warn">用户名或密码错误!</div>   
  33.                 <div class="remember">   
  34.                     <label>   
  35.                         <input type="checkbox" name="remembered" checked />   
  36.                         自动登录</label>   
  37.                     <a class="forget" href='<%=Url.Action("ResetPwd","Login") %>' >忘记密码?</a>   
  38.                 </div>   
  39.                 <input class="loginBtn" type="button" value="登录"/>   
  40.             </div>   
  41.             </form>   
  42.         </div>   
  43.     </div>   
  44. </body>   
  45. <script type="text/javascript">   
  46.     $(function () {   
  47.         $('.userId,.passwordStyle').on('keyup'function (e) {   
  48.             if (e.keyCode == 13) {   
  49.                 $('.loginBtn').trigger('click');   
  50.             }   
  51.         });   
  52.         $('.loginBtn').on('click'function () {   
  53.             $(".warn").hide();   
  54.             var pwd = $('.passwordStyle').val();   
  55.             if (pwd == '') {   
  56.                 $(".warn").show().html('请输入密码');   
  57.                 return false;   
  58.             }   
  59.             var data = $("#formData").serialize();   
  60.             $.post("/login/checkLoginInfo", data, function (ajaxObj) {   
  61.                 //回传内容{status: 1(success)/0(fail),}   
  62.                 if (ajaxObj.status == 0 || status == null) {   
  63.                     $(".warn").show().html('用户名或密码错误!');   
  64.                 } else {   
  65.                     //登陆成功,跳转都制定页面   
  66.                     window.location = '/memberCenter/index';   
  67.                 }   
  68.             }, "json");   
  69.         });   
  70.     });   
  71. </script>   
  72. </html> 

控制器

复制代码代码如下:
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;  
            }  
        }  
    }  
}

 

希望本文所述对大家的.NET程序设计有所帮助。

标签:

相关文章

热门资讯

玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
配置IIS网站web服务器的安全策略配置解决方案
配置IIS网站web服务器的安全策略配置解决方案 2019-05-23
Nginx服务器究竟是怎么执行PHP项目
Nginx服务器究竟是怎么执行PHP项目 2019-05-24
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情 2019-06-22
返回顶部