简体   繁体   中英

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".

Here is the 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

// 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

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

And declare an application variable in Global.asax in

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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