.net的ajax請(qǐng)求數(shù)據(jù)提交實(shí)例

字號(hào):


    本文實(shí)例講述了.net的ajax請(qǐng)求數(shù)據(jù)提交實(shí)現(xiàn)方法。分享給大家供大家參考。具體如下:
    復(fù)制代碼 代碼如下:<%@ page language=c# inherits=system.web.mvc.viewpage<dynamic> %>
    <head runat=server>
    <title>ajax請(qǐng)求</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+導(dǎo)航-->
    <div class=logo_box>
    <div id=logo>
    <a title=ajax請(qǐng)求>ajax請(qǐng)求</a></div>
    </div>
    <!---->
    <div class=logincon>
    <div class=loginbanner>
    <img src=/images/4499633_182932517000_2.jpg /></div>
    <div class=loginbox>
    <h2>
    <span class=fl>會(huì)員登錄</span><span class=newuser>沒有賬號(hào)?<a href='<%=url.action(register,account) %>'>立即注冊(cè)</a></span></h2>
    <form id=formdata>
    <div class=loginform>
    <div class=inputbox>
    <input type=text name=user value=用戶名/手機(jī)號(hào) class=userid />
    </div>
    <div class=inputbox>
    <input type=text value=密碼 class=textstyle />
    <input type=password name=pwd class=passwordstyle none />
    </div>
    <div class=warn>用戶名或密碼錯(cuò)誤!</div>
    <div class=remember>
    <label>
    <input type=checkbox name=remembered checked />
    自動(dòng)登錄</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('請(qǐng)輸入密碼');
    return false;
    }
    var data = $(#formdata).serialize();
    $.post(/login/checklogininfo, data, function (ajaxobj) {
    //回傳內(nèi)容{status: 1(success)/0(fail),}
    if (ajaxobj.status == 0 || status == null) {
    $(.warn).show().html('用戶名或密碼錯(cuò)誤!');
    } else {
    //登陸成功,跳轉(zhuǎn)都制定頁(yè)面
    window.location = '/membercenter/index';
    }
    }, json);
    });
    });
    </script>
    </html>
    控制器
    復(fù)制代碼 代碼如下: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();
    //從數(shù)據(jù)庫(kù)讀取
    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:登陸成功,記錄登陸用戶信息保存登陸狀態(tài)
    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;
    }
    }
    }
    }
    希望本文所述對(duì)大家的.net程序設(shè)計(jì)有所幫助。