前台JS
$.ajax({
type: ‘post‘,
url: ‘HandlerLogin.ashx‘,
data: { name: $( ‘#txtLoginName‘).val(), pwd: $(‘#txtPwd‘ ).val() },
dataType: ‘json‘,
success: function (result) {
loginAndRegDialog.dialog( ‘close‘);
$.messager.show({
title: ‘提示‘,
msg: ‘登陆成功‘
});
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
console.info(XmlHttpRequest);
console.info(textStatus);
console.info(errorThrown);
}
});
后台处理:
public class HandlerLogin : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
if (!string .IsNullOrEmpty(context.Request["name"]))
{
string name = context.Request["name" ];
context.Response.Write(LoginValidate(name));
}
else {
return;
}
}
/// <summary>
/// 根据登陆名判断时候存在该用户
/// </summary>
/// <param name="loginName"></param>
/// <returns> 返回json字符串 </returns>
private string LoginValidate(string loginName) {
string json="" ;
string constr = ConfigurationManager .ConnectionStrings["miceSysbase"].ToString();
using (SqlConnection conn=new SqlConnection(constr))
{
string sql = "select * from Aut_User where LoginInfo=‘" + loginName + "‘";
SqlDataAdapter sa = new SqlDataAdapter(sql, conn);
DataTable dt = new DataTable();
sa.Fill(dt);
if (dt.Rows.Count>0)
{
DataRow dr = dt.Rows[0];
json = ViewToJsonData(dr);
}
else {
json = "{‘false‘:[{‘UserID‘:‘‘,‘LoginInfo‘:‘‘,‘UserName‘:‘‘,‘CreateTime‘:‘‘}]}" ;
}
}
return json;
}
/// <summary>
/// 根据查询结果序列化数据
/// </summary>
private string ViewToJsonData(DataRow dr)
{
string json = "{\"UserID\":\"" + dr["UserID"].ToString() + "\",\"LoginInfo\":\"" + dr["LoginInfo" ].ToString() + "\",\"UserName\":\"" + dr["UserName" ].ToString() + "\",\"CreateDate\":\"" + dr["CreateDate" ].ToString() + "\"}";
return json;
}
}