手机
当前位置:查字典教程网 >编程开发 >asp.net教程 >.NET的Ajax请求数据提交实例
.NET的Ajax请求数据提交实例
摘要:本文实例讲述了.NET的Ajax请求数据提交实现方法。分享给大家供大家参考。具体如下:复制代码代码如下:ajax请求ajax请求会员登录没有...

本文实例讲述了.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="http://www.jb51.netScripts/jquery-1.8.3.min.js"></script>

<script type="text/javascript" src="http://www.jb51.netScripts/js.js"></script>

</head>

<body>

<>

<div>

<div id="logo">

<a title="ajax请求">ajax请求</a></div>

</div>

<>

<div>

<div>

<img src="http://www.jb51.netImages/4499633_182932517000_2.jpg" /></div>

<div>

<h2>

<span>会员登录</span><span>没有账号?<a href='<%=Url.Action("Register","Account") %>'>立即注册</a></span></h2>

<form id="formData">

<div>

<div>

<input type="text" name="user" value="用户名/手机号" />

</div>

<div>

<input type="text" value="密码" />

<input type="password" name="pwd" />

</div>

<div>用户名或密码错误!</div>

<div>

<label>

<input type="checkbox" name="remembered" checked />

自动登录</label>

<a href='<%=Url.Action("ResetPwd","Login") %>' >忘记密码?</a>

</div>

<input 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程序设计有所帮助。

【.NET的Ajax请求数据提交实例】相关文章:

ASP.NET封装的SQL数据库访问类

ASP.NET2.0中数据源控件之异步数据访问

html+ashx 表单提交示例

ASP.NET Dictionary 的基本用法示例介绍

ASP.NET缓存处理类实例

ASP.NET中 Execl导出的六种方法实例

.NET 数据库连接池

.NET 日志系统设计思路及实现代码

.NET 扩展实现代码

ASP.NET中实现根据匿名类、datatable、sql生成实体类

精品推荐
分类导航