我一直在寻找如何做到这一点-我试图在删除请求后进行重定向-这是我使用的代码没有重定向:
exports.remove = function(req, res) {
var postId = req.params.id;
Post.remove({ _id: postId }, function(err) {
if (!err) {
console.log('notification!');
res.send(200);
}
else {
console.log('error in the remove function');
res.send(400);
}
});
};
当条目(post)被删除时,调用 remove
。一切工作正常(我不得不使用res.send(200)
让它不挂在删除请求)-但现在我有重定向的麻烦。如果我在remove
函数内使用res.redirect('/forum')
,如下所示:
exports.remove = function(req, res) {
var postId = req.params.id;
Post.remove({ _id: postId }, function(err) {
if (!err) {
console.log('notification!');
res.send(200);
}
else {
console.log('error in the remove function');
res.send(400);
}
res.redirect('/forum');
});
};
它将重定向注册为试图删除/forum
的DELETE
请求,如下:
DELETE http://localhost:9000/forum 404 Not Found 4ms
我所要做的就是刷新页面,以便在删除后更新帖子列表。有人能帮忙吗?
我知道这有点晚了,但是对于以后看到这个的人来说,您还可以手动将HTTP方法重置为GET,这也应该可以工作
exports.remove = function(req, res) {
var postId = req.params.id;
Post.remove({ _id: postId }, function(err) {
if (!err) {
console.log('notification!');
res.send(200);
}
else {
console.log('error in the remove function');
res.send(400);
}
//Set HTTP method to GET
req.method = 'GET'
res.redirect('/forum');
});
};
@ewizard的解决方案是伟大的,如果你能在前端解决这个问题。但是,如果您想在后端解决这个问题,您可以向res.redirect
添加一个可选的状态码参数,如下所示:
res.redirect(303, "/forum");
此重定向为"未定义原因",默认为GET重定向。
我让它在我的Angular侧与$window.location.href = '/forum';
一起工作-只需将它放在$http
请求的成功函数中,该请求是delete
函数的一部分,当单击"Delete"按钮时执行