.NET 의 Ajax 요청 데이터 제출 실례

8207 단어 .NETAjax
이 글 은.NET 의 Ajax 요청 데이터 제출 실현 방법 을 실례 로 서술 하 였 다.모두 에 게 참고 하도록 공유 하 다.구체 적 으로 다음 과 같다.
" %>  
 
<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 프로 그래 밍 에 도움 이 되 기 를 바 랍 니 다.

좋은 웹페이지 즐겨찾기