AsyncData.ashx檔案
public void ProcessRequest (HttpContext context) {
string action = context.Request.QueryString["action"];//getcity,getstore
string arg = context.Request.QueryString["arg"];// areaGUID,
string pwd = context.Request.QueryString["pwd"];//user login password
StringBuilder strResult = new StringBuilder();
if (action == "userLogin")
{
Model.BJ_UserInfo userModel = new Model.BJ_UserInfo();
BLL.BJ_User userBll = new BLL.BJ_User();
UserInfo ui = new UserInfo();
if (string.IsNullOrEmpty(arg) || string.IsNullOrEmpty(pwd))
return;
else
userModel = userBll.GetModel(arg, WebUtility.GetMD5(pwd));
if (userModel != null)
{
ui.UserGUID = userModel.UserGUID.ToString();
ui.UserName = userModel.UserName;
ui.RealName = userModel.RealName;
ui.SecurityGUID = userModel.SecurityGUID.ToString();
//ui.IP = WebUtility.GetIpAddress;
//ui.StoreName = IpLocation(ui.IP);
context.Session["userinfo"] = ui;
strResult.Append(ui.UserName);
}
else
strResult.AppendFormat("0");
}
}
CheckLogin.js檔案
//---------------------javascript-------------------------------
jQuery(function($){
$("#btnLogin").click(function(){
var target1 = $("#txtUserName");
var target2 = $("#txtPwd");
if(target1.val()=='')
{
alert('使用者名稱不可為空!');
return;
}
if(target2.val()=='')
{
alert('密碼不可為空!');
return;
}
else
funAjaxText("userLogin", target1.val(), target2.val());
});
var funAjaxText = function(action, username,pwd){
$.ajax({
type: "GET",
url: "../Controls/AsyncData.ashx?action="+ action +"&arg="+username+"&pwd="+pwd,
beforeSend: function(data){
},
success: function(data){
if(data=="0")
{
$("#txtPwd").val("");
alert("使用者名稱或密碼有誤!");
}
else
{
window.location.href="MyBJ/MyBJ.aspx";
}
}
});
};
});
首頁使用者登入,進行退出時,清空當前會話
Session.Clear();
Session.Abandon();
Response.Redirect("index.aspx");
並跳轉到前頁(重新整理),
再次登入時,登入成功,跳轉到mybj.aspx頁面
page_load()
{
UserInfo userModel = new UserInfo();
userModel = (UserInfo)Session["userinfo"];
if (userModel == null)
{
WebUtility.ShowMessageAndRedirect("你還沒有登入!","../index.aspx", this.Page);
}
}
會出現,session["userinfo"]為空白的情況。
這裡的問題是緩衝,
解決方案是在進行非同步資料請求時,加多一個隨機數,以使此請求不同於前,於是才不會去讀取緩衝。
url: "../Controls/AsyncData.ashx?action="+ action +"&arg="+username+"&pwd="+pwd+"&update="+Math.random(),