一直致力于檢測 asp.net 專案的超時。有一個 ajax 函式,每 5 秒檢查一次會話是否已過期。如果沒有 ajax 功能檢查,它實際上會在一分鐘后過期,但如果我保持該功能開啟,它會不斷向我發送“活動”狀態。所以我想知道,我的 ajax 函式/請求是否使會話保持活動狀態?
阿賈克斯功能:
function isSessionAlive(){
await jQuery.ajax({
type: "POST",
url: 'coolpage.aspx/hello',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
console.info("status: ", response);
},
failure: function (response) {
console.info("status: ", response);
},
cache:false
});}
asp.net頁面方法
//[WebMethod(EnableSession = true)]
[WebMethod]
public static string hello()
{
//return (HttpContext.Current.Session["dummy"] == null) ? "expired" : "active";
if (HttpContext.Current.Session != null)
{
if (HttpContext.Current.Session.IsNewSession)
{
string cookieHeader = HttpContext.Current.Request.Headers["Cookie"];
if ((null != cookieHeader) && (cookieHeader.IndexOf("ASP.NET_SessionId") >= 0))
{
return "expired";
}
}
}
return "active";
// return HttpContext.Current.User.Identity.IsAuthenticated ? "active":"expired";
}
uj5u.com熱心網友回復:
您必須在 ajax 中使用 setInterval 函式。
setInterval(function(){
$.get('coolpage.aspx/hello');
}, 300000); // 5mins
uj5u.com熱心網友回復:
如果您的會話已過期,那么銷毀您的會話并回傳“過期”。呼叫 ajax(document.ready) 后,您必須使用 if 和 else 檢查會話值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/339644.html
標籤:javascript C# asp.net 阿贾克斯 会议
下一篇:while陳述句中錯誤的日期增量
