.NET的Ajax请求数据提交实例
网络编程 2021-07-04 22:41www.168986.cn编程入门
这篇文章主要介绍了.NET的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="/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","Aount") %>'>立即注册</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="fet" 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(suess)/0(fail),}
if (ajaxObj.status == 0 || status == null) {
$(".warn").show().html('用户名或密码错误!');
} else {
//登陆成功,跳转都制定页面
window.location = '/memberCenter/index';
}
}, "json");
});
});
</script>
</html>
<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","Aount") %>'>立即注册</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="fet" 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(suess)/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 aount = MemberInfoService.GetMemberIdForCheck(user, pwd);
if (aount == null)
{
res.Data = new { status = 0 };
}
else
{
//{status: 1(suess)/0(fail),}
res.Data = new { status = 1 };
//todo登陆成功,记录登陆用户信息保存登陆状态
FunSession.SetSession(aount);
//是否记住登录
if (remembered == "on")
{
HttpCookie cookie = new HttpCookie("LoginInfo", aount.Id.ToString());
//3天有效
cookie.Expires.AddDays(3);
Response.Cookies.Add(cookie);
}
else
{
HttpCookie cookie = new HttpCookie(aount.Id.ToString(), aount.Id.ToString());
//使失效
cookie.Expires.AddYears(-1);
Response.Cookies.Add(cookie);
}
}
return res;
}
catch (Exception ex)
{
throw ex.InnerException;
}
}
}
}
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 aount = MemberInfoService.GetMemberIdForCheck(user, pwd);
if (aount == null)
{
res.Data = new { status = 0 };
}
else
{
//{status: 1(suess)/0(fail),}
res.Data = new { status = 1 };
//todo登陆成功,记录登陆用户信息保存登陆状态
FunSession.SetSession(aount);
//是否记住登录
if (remembered == "on")
{
HttpCookie cookie = new HttpCookie("LoginInfo", aount.Id.ToString());
//3天有效
cookie.Expires.AddDays(3);
Response.Cookies.Add(cookie);
}
else
{
HttpCookie cookie = new HttpCookie(aount.Id.ToString(), aount.Id.ToString());
//使失效
cookie.Expires.AddYears(-1);
Response.Cookies.Add(cookie);
}
}
return res;
}
catch (Exception ex)
{
throw ex.InnerException;
}
}
}
}
希望本文所述对大家的.NET程序设计有所帮助。
上一篇:asp.net+ajax的Post请求实例
下一篇:.NET中的属性用法分析
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程