为什么res.redirect()无法刷新当前页面?
发布于 9 年前 作者 townmi 9981 次浏览 最后一次编辑是 8 年前 来自 问答
router.get("/", function (req, res){
	pool.getConnection(function (err, connection) {
		connection.query('SELECT * FROM pro', function (err, rows) {
			res.render("index",{"rows" : rows});
		});
	});
})
// delete
router.post('/delete', function (req, res){
	pool.getConnection(function (err, connection) {
		connection.query("DELETE FROM pro WHERE id = "+req.body.del, function (err, rows) {
			connection.release();
			res.redirect(301,"/");
		});
	});
});

res.redirect(301, "/")之后Response的代码的确已经更新;但是页面没有重新渲染。问什么啊?

5 回复
var counter=0;
router.get("/", function (req, res){
	console.log(++n);
    pool.getConnection(function (err, connection) {
        connection.query('SELECT * FROM pro', function (err, rows) {
            res.render("index",{"rows" : rows});
        });
    });
})

这样追踪一下 看看

@dayuoba 1.png res.redirect()是走了app.get("/",.....)的,但是页面的确没有改动,需要F5才能,你懂的。

我好像明白了,因为我的当前目录就是“/”,从根目录重定向到“/”。window.location没有变啦,所以没有刷新。。。 这么解释,比较合理。

嗯,不过想刷新试试用render不用redirect

还是不行,从“/edit"路径redirect到“/”,也不能刷新,难不成重定向就不带刷新的。express版本4.9.7

回到顶部