node.js Express js - 无法重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8240447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Express js - can't redirect
提问by Chin
I am trying to do the following:
我正在尝试执行以下操作:
from client
来自客户
var req = jQuery.post(
"http://www.example.com:3000"+"/dologin",
{"username" : username, "password" : password}).error(function(){
alert("an error occurred");
});
in express root
在快速根
app.post('/dologin',function(req, res) {
res.redirect('http://bbc.co.uk');
});
result passed back
结果传回来
<p>Moved Temporarily. Redirecting to <a href="http://bbc.co.uk">http://bbc.co.uk</a></p>
Seems that if I do post from jquery the redirect will not work. Does anyone know a way to force it to redirect?
似乎如果我从 jquery 发帖,重定向将不起作用。有谁知道强制它重定向的方法?
回答by Teemu Ikonen
Browser does not redirect the window on redirect on ajax response. Redirect the browser with javascript.
浏览器不会在重定向 ajax 响应时重定向窗口。使用 javascript 重定向浏览器。
In server send the new site as content, for example.
例如,在服务器中将新站点作为内容发送。
res.contentType('application/json');
var data = JSON.stringify('http://site.example.com/')
res.header('Content-Length', data.length);
res.end(data);
In client
在客户端
var req = jQuery.post(
"http://www.mysite.com:3000"+"/dologin",
{"username" : username, "password" : password}, 'json').error(function(){
alert("an error occurred");
}).success(function(data) {
window.location = data;
});
回答by alessioalex
I've actually encountered the same thing when developing an app. It seems Express doesn't redirect if the method is post.
我实际上在开发应用程序时遇到过同样的事情。如果方法是 post,似乎 Express 不会重定向。
Try:
尝试:
app.post('/dologin',function(req, res) {
req.method = 'get';
res.redirect('http://bbc.co.uk');
});
回答by Paul S Chapman
I'm doing something like this when using OAuth2. I have an link to one of my pages and this in turn redirects to google.
我在使用 OAuth2 时正在做这样的事情。我有一个链接到我的一个页面,这反过来又重定向到谷歌。
To redirect to another location the following code does the actual redirect
要重定向到另一个位置,以下代码会执行实际的重定向
app.get('/GoogleRequestAuthorization.html',function(req,res) {
.
.
.
.
res.writeHead(302, {location: url});
res.end();
});
});
url being the address you want to redirect to.
url 是您要重定向到的地址。
The full function is...
完整的功能是...
回答by KirilStankov
I have come to a similar problem and solved it by checking the type of the request. In my case I use JSON, but it works for other POST requests as well:
我遇到了类似的问题,并通过检查请求的类型来解决它。就我而言,我使用 JSON,但它也适用于其他 POST 请求:
var ajax = req.xhr;
if(ajax) {
res.status(401).json({'msg':'redirect','location':'/login'});
}
else {
req.method = 'get';
res.status(401).redirect('/login');
//Or if you prefer plain text
//res.status(333).send("Redirect");
}
This handles both Ajax POST and AJAX and standard GET requests. On the client, in the Ajax respose callback:
这可以处理 Ajax POST 和 AJAX 以及标准的 GET 请求。在客户端,在 Ajax respose 回调中:
$.ajax({
type: 'POST',
data: Msg,
url: '/some/post',
dataType: 'JSON'
}).success(function(data, textStatus, req ) {
if(data.msg==="redirect") window.location = data.location;
else {...}
}).error(function(data, textStatus, req) {
if(req=="Unauthorized") {
alert("Unauthorized!");
window.location = "/login";
} else if (data.responseJSON.msg==="redirect") window.location = data.responseJSON.location;
else {
//...
}
});
You can actually handle more statuses here, except for 302, which is automatically followed by JQuery and you get as response 200 from the page you wanted to redirect to. So I avoid sending the 302, sending 401 in my case, or any other status, for example 333, which will be handled as error.
您实际上可以在此处处理更多状态,但 302 除外,它会自动跟随 JQuery 并且您从要重定向到的页面获得响应 200。所以我避免发送 302,在我的情况下发送 401 或任何其他状态,例如 333,它们将被处理为错误。

