ExpressJS-删除请求后的res.redirect [英] ExpressJS - res.redirect after DELETE request

查看:100
本文介绍了ExpressJS-删除请求后的res.redirect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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)使其不挂在删除请求上)-但现在我在重定向时遇到了麻烦.如果我在remove函数内使用res.redirect('/forum'),则像这样:

remove gets called when an item (a post) is deleted. 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. If I use res.redirect('/forum') inside the remove function, like this:

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

它将重定向注册为尝试删除/forumDELETE请求,如下所示:

It registers the redirect as a DELETE request that is trying to delete /forum, like this:

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?

推荐答案

我将它与$window.location.href = '/forum';一起在Angular上使用-只需将其放在delete请求的成功函数中,该请求是单击删除"按钮时执行的功能.

I got it working on my Angular side with $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.

这篇关于ExpressJS-删除请求后的res.redirect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆