繁体   English   中英

重定向到另一个无法在javascript中运行的页面

[英]Redirecting to another page not working in javascript

嗨伙计。 在我的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>

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

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

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 );

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

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

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

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

现在,我可以在整个站点中使用变量了。 在这种情况下,您可以简单地使用

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM