欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-22 10:48:22
本文实例讲述了.net的ajax请求数据提交实现方法。分享给大家供大家参考。具体如下: 复制代码 代码如下:<%@ page language="c#" inher...

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

复制代码 代码如下:
<%@ 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>

控制器

复制代码 代码如下:
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程序设计有所帮助。