[英]Redirecting to another page not working in javascript
Hi folks. 嗨伙计。 In my MVC application, I am trying to redirect to a login page, however it is not redirecting and I am getting a "server error". 在我的MVC应用程序中,我试图重定向到登录页面,但是它没有重定向,并且出现“服务器错误”。
Here is the javascript: 这是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>
Also, I need the code for if the user presses the 'ok' button and it continues. 另外,如果用户按下“确定”按钮并继续,我需要代码。
Make sure that the location you're redirecting to includes the protocol ie 确保您重定向到的位置包括协议,即
window.location.href = 'http://www.yoursite.tld/account/logout';
For the second bit you can make an ajax call to a heartbeat page to refresh the session 第二点,您可以对心跳页面进行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 );
In my case, I prefer to add the full path link to web.config in 就我而言,我更喜欢将完整路径链接添加到web.config中
<appSettings>
<add key="BaseURL" value="http://localhost/" />
</appSettings>
And declare an application variable in Global.asax in 并在Global.asax中声明一个应用程序变量
protected void Application_Start()
{
Application["BaseURL"] = System.Configuration.ConfigurationManager.AppSettings["BaseURL"];
}
And now I can use the variables in the whole site. 现在,我可以在整个站点中使用变量了。 In you case, you can simply use by 在这种情况下,您可以简单地使用
window.location.href = '<%=Application["BaseURL"]%>account/logout';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.