Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ExpressJS-res.DELETE请求后重定向_Javascript_Redirect_Express_Request_Http Delete - Fatal编程技术网

Javascript ExpressJS-res.DELETE请求后重定向

Javascript ExpressJS-res.DELETE请求后重定向,javascript,redirect,express,request,http-delete,Javascript,Redirect,Express,Request,Http 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);

我一直在搜索如何执行此操作-我正在尝试在发出删除请求后重定向-以下是我正在使用的没有重定向的代码:

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')
,如下所示:

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
请求,如下所示:

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');
  });
};
删除http://localhost:9000/forum 404找不到4ms


我所要做的就是刷新页面,以便在删除后更新帖子列表。有人能帮忙吗

我用
$window.location.href='/forum'让它在我的角度上工作-只需将其放入
$http
请求的成功函数中,该函数是
删除
函数的一部分,单击“删除”按钮时将执行该函数。

我知道这已经晚了,但对于稍后看到该操作的任何人,您也可以手动重置http方法以获取该方法也可以工作的内容

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重定向


有关更多信息,请参阅。

如果您需要删除某些内容而不首先出现在页面上,该怎么办?@trysis-这是一个问题吗?你是有问题还是想指出我做错了什么?已经有一段时间了,但对于这个问题,我不需要在不进入页面的情况下删除帖子…如果我想,我可以更手动地从数据库中删除它,因为我是管理员…哦,对不起。对于大多数网站(我想,包括你的网站)来说,这没关系。这只是对一些人来说是不可能的,而我正试图让问题变得更广泛。别介意我。:)我想做重定向从服务器端,而不是必须发送一些对象的指示客户端重定向。。。有人知道怎么做吗?我和你有同样的问题。一切都很好。但是
res.redirect
不重新加载页面。@novaline-您也使用angular吗?最后,我不得不在前端做一些事情,而不是在快速端——见下面的答案。不,我使用jQuery。我想在服务器端重新加载页面。真奇怪。当我执行
create
操作(POST、表单操作)时,
res.redirect
起作用。但是
delete
操作(POST,$.ajax),
res.redirect
很好,没有错误。但问题是它不能刷新页面。我可以看到从服务器端返回的网络
响应
。请尝试使用
window.location.href=url在jquery端。这是直接的javascript…我认为jquery版本应该是
$(window.location.href=
,但两者应该做相同的事情<代码>url
是您希望重定向到的位置。你只需要把它放在正确的地方。还有jquery。如果你愿意,我可以帮你找到合适的地方。您可以使用它而不是服务器端res.redirect您只需通过服务器端的消息触发它即可。谢谢。但我的问题是,为什么浏览器不刷新页面,然后我在服务器端调用
res.redirect
。http状态为200,您可以看到服务器端返回的响应(html
html
文档)。我知道如何在客户端完成它。因为使用了ajax
?也许
表单操作
请求可以吗?