res.redirect 从 POST [英] res.redirect from POST
本文介绍了res.redirect 从 POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于某种原因,我无法在登录完成后重定向到/blog.在我的登录控制器中,我有以下内容.
For some reason I cant redirect to /blog once my login is completed. In my login controller I have the following.
module.exports = {
post: function(req, res) {
var login = req.body['login'];
if (login && req.body['login']['password'] == "password") {
console.log('Granted access');
res.send({redirect: '/blog'});
}
else {
console.log('wrong password');
res.redirect('back');
}
}
};
jquery ajax
The jquery ajax
$(document).ready ->
$('#login-button').click () ->
$.ajax
url: '/login'
type: 'POST'
data: $('#Password').serialize()
dataType: 'json'
success: (data, textStatus, jqXHR) ->
if typeof data.redirect == 'string'
window.location = data.redirect
更新到工作代码
推荐答案
您不能在 AJAX 之后进行重定向.你需要自己用 Javascript 来完成.
You can't make a redirection after an AJAX. You need to do it yourself in Javascript.
服务器
post: function(req, res) {
var login = req.body['login'];
app.use(express.bodyParser());
if (login && req.body['login']['password'] == "tom") {
var loginPassword = req.body['login']['password'];
console.log(loginPassword);
console.log('Granted access');
res.send({redirect: '/blog'});
}
...
}
客户
$(document).ready ->
$('#login-button').click () ->
$.ajax
url: '/login'
type: 'POST'
data: $('#Password').serialize()
dataType: 'json'
success: (data, textStatus, jqXHR) ->
if typeof data.redirect == 'string'
window.location = data.redirect
这应该可行.
这篇关于res.redirect 从 POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文