ajax配合一般处理程序(.ashx)登录的一般写法
2018-06-17 21:42:09来源:未知 阅读 ()
前端:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>首页</title>
<script type="text/javascript" src="JQuery/jquery.min.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" id="txtlogin" value="车辆管理员1" />
<input type="button" onclick="Login()" value="登录" />
</div>
</form>
<script type="text/javascript">
function Login() {
//var userid = $("#txtlogin").val();
$.ajax({
type: "post",
url: "CarManager/ashx/User.ashx",
data: { "action": "userlogin", "username": $("#txtlogin").val() },
dataType: "json",
success: function (data) {
if (data.msg="1") {
location.href = "CarManager/Main.aspx";
}
}
});
}
</script>
</body>
</html>
后端:
public class User : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
StringBuilder _strContent = new StringBuilder();
if (_strContent.Length == 0)
{
string _strAction = context.Request.Params["action"];
if (string.IsNullOrEmpty(_strAction))
{
_strContent.Append("{\"msg\": \"0\", \"msgbox\": \"禁止访问!\",\"rows\": []}");
}
else
{
switch (_strAction.Trim().ToLower())
{
case "userlogin": _strContent.Append(UserLogin(context)); break;
default: break;
}
}
}
context.Response.Write(_strContent.ToString());
}
private string UserLogin(HttpContext context)
{
string result = "";
string _username = context.Request.Form["username"];
string _password = context.Request.Form["password"];
Model.cmUser model = new Model.cmUser();
if (context.Session["UserModel"] != null)
{//当前浏览器已经有用户登录 判断是不是当前输入的用户
model = (Model.cmUser)context.Session["UserModel"];
if (model.Name != _username)
{
result = "{\"msg\": \"0\", \"msgbox\": \"此浏览器已经有其他用户登录!\"}";
}
else
{
result = "{\"msg\": \"1\", \"msgbox\": \"登录成功!\"}";
}
}
else
{
BLL.cmUser bll = new BLL.cmUser();
string strWhere = string.Format("[Name]='{0}'", _username);// and [Password]='{1}', _password
DataTable dt = bll.GetList(1, strWhere, " ID ").Tables[0];
if (dt != null)
{//用户和密码正确
int _userid = 0;
int.TryParse(dt.Rows[0]["ID"].ToString(), out _userid);
model.ID = _userid;
model.Name = dt.Rows[0]["Name"].ToString();
int _type = 0;
int.TryParse(dt.Rows[0]["Type"].ToString(), out _type);
model.Type = _type;
context.Session["UserModel"] = model;
result = "{\"msg\": \"1\", \"msgbox\": \"登录成功!\"}";
}
else
{
result= "{\"msg\": \"0\", \"msgbox\": \"用户名或密码错误!\"}"; ;
}
}
return result;
}
public bool IsReusable
{
get
{
return false;
}
}
}
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:C#窗体布局方式
下一篇:一个net程序猿必备工具
- mysql登录时闪退 2020-02-27
- PHP 实例 - AJAX 实时搜索 2019-09-17
- PHP+Ajax实现的博客文章添加类别功能 2019-08-09
- MySQL数据库表设计规范 2019-07-24
- MySQL数据库换挡加速的方式 2019-05-08
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
