简体   繁体   English

ExpressJS - DELETE请求后的res.redirect

[英]ExpressJS - res.redirect after DELETE request

I have been searching all over for how to do this - I am trying to redirect after a DELETE request is made - here is the code I am using WITHOUT THE REDIRECT : 我一直在搜索如何执行此操作 - 我在尝试DELETE请求后重定向 - 这是我正在使用的代码没有重定向

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);
    }
  });
};

remove gets called when an item (a post) is deleted. remove项目(帖子)时调用remove Everything works fine (I had to use res.send(200) to get it to not hang on the delete request) - but now I am having trouble redirecting. 一切正常(我不得不使用res.send(200)让它不挂在删除请求上) - 但现在我无法重定向。 If I use res.redirect('/forum') inside the remove function, like this: 如果我在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');
  });
};

It registers the redirect as a DELETE request that is trying to delete /forum , like this: 它将重定向注册为试图删除/forumDELETE请求,如下所示:

DELETE http://localhost:9000/forum 404 Not Found 4ms

All I am trying to do is refresh the page so that the list of posts is updated after the delete. 我要做的就是刷新页面,以便在删除后更新帖子列表。 Can anyone help? 有人可以帮忙吗?

I know this is late, but for anyone who sees this later, you can also manually reset the HTTP method to GET which should also work 我知道这已经很晚了,但是对于后来看到这个的人来说,你也可以手动将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 's solution is great if you can fix this on the front end. @ewizard的解决方案很棒,如果你能在前端解决这个问题。 However, if you want to fix this on the back end, you can add an optional Status Code argument to res.redirect like so: 但是,如果要在后端修复此问题,可以向res.redirect添加可选的“状态代码”参数, res.redirect所示:

res.redirect(303, "/forum");

This redirects for "Undefined Reason" which will default to a GET redirect. 这会重定向“未定义的原因”,默认为GET重定向。

See this SO post for more info. 有关详细信息,请参阅此SO帖子

I got it working on my Angular side with $window.location.href = '/forum'; 我使用$window.location.href = '/forum';在我的Angular方面工作了$window.location.href = '/forum'; - just put it in the success function of the $http request that is part of the delete function that gets executed when the "Delete" button is clicked. - 只需将它放在$http请求的成功函数中,该函数是单击“删除”按钮时执行的delete功能的一部分。

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

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