如何在.NET中使用Ajax请求提交数据

发布时间:2021-03-04 16:48:53 作者:Leah
来源:亿速云 阅读:144

如何在.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"> 
            <h3> 
                <span class="fl">会员登录</span><span class="newUser">没有账号?<a href='<%=Url.Action("Register","Account") %>'>立即注册</a></span></h3> 
 
            <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中使用Ajax请求提交数据的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. Ajax表单提交数据
  2. Ajax提交数据

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ajax

上一篇:在CI框架中加载session时出现报错如何解决

下一篇:Linux怎么安装.Net core 环境和运行

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》