我正在使用下面的文章在我的asp.net应用程序中保持会话活动。
但是,当我在javascript中使用alert检查会话变量值时,我没有发现任何东西。alert('<%=Session["Heartbeat"]%>');
实际上,在我的网页上没有回发。我在Jquery Ajax上所做的所有工作。只有当有任何回发时,该文章的解决方案才起作用。但是我想在没有任何回发的情况下保持会话的活动状态。
请帮帮我。
发布于 2016-04-27 12:31:31
没有完全回发,alert('<%=Session["Heartbeat"]%>');将无法工作,因为<%=Session["Heartbeat"]%>是服务器端代码。
为了让会话保持活动状态,您只需要对服务器执行一个简单的Ajax调用。
实现ASHX通用处理程序的最简单方法,并通过Ajax调用它。例如,
Ping.ashx
<%@ WebHandler Language="C#" CodeBehind="Ping.ashx.cs" Class="PROJECT_NAMESPACE.Ping" %>Ping.ashx.cs
public class Ping : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write("Ping");
}
public bool IsReusable { get { return false; }}
}发布于 2016-04-27 12:17:04
请尝试此example :-
<script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script language="javascript" type="text/javascript">
function KeepSession() {
// A request to server
$.post("http://servername:port/appName/SessionCheck.aspx");
//now schedule this process to happen in some time interval, in this example its 1 min
setInterval(KeepSession, 60000);
}
// First time call of function
KeepSession();
</script>https://stackoverflow.com/questions/36879867
复制相似问题