我一直在寻找如何执行此操作的方法-发出DELETE请求后,我试图重定向-这是我使用的代码,没有REDIRECT :

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。一切正常(我不得不使用res.send(200)使其不卡在删除请求上)-但现在我在重定向时遇到了麻烦。如果我在res.redirect('/forum')函数中使用remove,则如下所示:
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');
  });
};

它将重定向注册为尝试删除DELETE/forum请求,如下所示:
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');
  });
};

09-17 15:35
查看更多