【问题标题】:Redirecting to another page not working in javascript重定向到另一个页面在 javascript 中不起作用
【发布时间】:2010-12-10 08:00:41
【问题描述】:

大家好。在我的 MVC 应用程序中,我试图重定向到登录页面,但是它没有重定向并且我收到“服务器错误”。

这里是javascript:

<script type="text/javascript"> 
function keepAlive() { 
window.clearTimeout(window.sessionKeepAlive); 
window.sessionKeepAlive = window.setTimeout(function() { 

    if(confirm('refresh session?')) { 
        // submit coding required 
    } else { 
        //window.location="/Employee/~/Account/LogOn"
        //location.replace("/Employee/~/Account/LogOn");
        window.location.href = '<%= Url.Action( "Logout", "Account" ) %>'; 
    } 

}, <%= (Session.Timeout - 19) * 60 * 1000 %>); 
} 
keepAlive(); 
</script>

另外,如果用户按下“确定”按钮并继续,我需要代码。

【问题讨论】:

  • 您遇到了服务器错误,真的,不是在开玩笑吗?让我们玩一个游戏:第一个猜出服务器错误的人赢得免费啤酒。你的最后一句话也很难理解。
  • 重定向后可能会出错
  • 当我点击取消按钮时,它应该进入登录页面,但它没有去

标签: javascript asp.net-mvc session-timeout


【解决方案1】:

就我而言,我更喜欢将完整路径链接添加到 web.config 中

<appSettings>
    <add key="BaseURL" value="http://localhost/" />
</appSettings>

并在Global.asax中声明一个应用变量

protected void Application_Start()
{
    Application["BaseURL"] = System.Configuration.ConfigurationManager.AppSettings["BaseURL"];
}

现在我可以在整个站点中使用变量了。在你的情况下,你可以简单地使用 by

window.location.href = '<%=Application["BaseURL"]%>account/logout';

【讨论】:

    【解决方案2】:

    确保您重定向到的位置包含协议,即

    window.location.href = 'http://www.yoursite.tld/account/logout';
    

    对于第二点,您可以对心跳页面进行 ajax 调用以刷新会话

    // simplified
    try {
        var xhr = new XMLHttpRequest();
    } catch( e ) {
        var xhr = new ActiveXObject('Microsoft.XMLHTTP');
    }
    
    xhr.open( 'get', 'http://heartbeat/url', true );
    xhr.send( null );
    

    【讨论】:

    • 我在本地运行如何提供链接'yoursite.tld/account/logout'
    • 我认为你可以修改路径名而不是window.location.pathname = '/account/logout'
    猜你喜欢
    • 1970-01-01
    • 2020-06-12
    • 2018-01-04
    • 2013-08-13
    • 2018-04-29
    • 1970-01-01
    • 1970-01-01
    • 2016-08-11
    • 2013-08-16
    相关资源
    最近更新 更多