简体   繁体   English

Nodejs Ajax - 重定向到登录页面

[英]Nodejs Ajax - redirect to the login page

I'm trying to solve this problem.我正在努力解决这个问题。 I use Node Express and jQuery. The user has been logged out (from another page of the site), but is trying to send an ajax request.我使用 Node Express 和 jQuery。用户已注销(从站点的另一个页面),但正在尝试发送 ajax 请求。 How to redirect it to the login page?如何将其重定向到登录页面? I can throw an error like so:我可以像这样抛出一个错误:

if (!req.isAuthenticated()) {
    return res.json(function() {});
}

and after on the client side:在客户端之后:

$.ajax({
    type: 'GET',
    ...
    error: function() {
        document.location.href = '/logout';
    }
})

But can it be done in some other way?但它能以其他方式完成吗? For example, if you do not use Ajax, then I do this:例如,如果您不使用 Ajax,那么我这样做:

if (!req.isAuthenticated()) {
    return redirect('/login');
}

Something similar but with Ajax?与 Ajax 类似的东西?

Since you mentioned you use Express, you can call the redirect function on the response:由于您提到您使用 Express,您可以在响应中调用重定向 function:

if (!req.isAuthenticated()) {
    res.redirect("/login");
}

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

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