[英]Nodejs page not redirecting after POST
My problem is similar enough to this but also different and the answers aren't working for me. 我的问题与此相似,但又有所不同,答案对我没有用。
I have a form to post and I need to display a message on the page whether it entered the data to the database successfully or not. 我有一个要发布的表单,无论页面是否成功将数据输入数据库,我都需要在页面上显示一条消息。 So I have: 所以我有:
app.post('/add_new', upload.single('image'), function add_new(req, res, err)
{
var image = req.body.image;
// do some checks and try to insert
if (err){
res.render('page_name',{images:images, message:'There was an error'})
}
else{
images.push(image);
res.render('page_name',{images:images, message: 'Success'})}
}
}
The error/success messages are handled on the client side. 错误/成功消息在客户端处理。
My problem is that it doesn't actually complete the post. 我的问题是,它实际上并未完成发布。 It renders the page with the error/success message correctly but the URL looks like 'my_site.com/add_new', and if the user refreshes the page for any reason, it sends that 'form resubmission' alert, which causes problems. 它正确显示了带有错误/成功消息的页面,但URL看起来像“ my_site.com/add_new”,并且如果用户出于任何原因刷新页面,则会发送“重新提交表单”警报,从而导致问题。 I need it to go back to 'my_site.com' and display the message. 我需要它返回“ my_site.com”并显示消息。
I have found that doing 我发现这样做
res.redirect('back')
returns to the correct page but obviously doesn't display the message. 返回正确的页面,但显然不显示该消息。 I have noticed a few answers recommending 我注意到一些建议的答案
res.end('Success')
but that just displays a white page with the word 'Success' - I don't know how anyone thinks that's an acceptable solution! 但这只会显示带有“成功”字样的白页-我不知道有人认为这是可以接受的解决方案!
EDIT: Following Vinay's answer, my code now reads like: 编辑:按照Vinay的回答,我的代码现在显示为:
app.post('/add_new', upload.single('image'), function add_new(req, res, err)
{
var image = req.body.image;
// do some checks and try to insert
if (err){
req.session.msg='error adding';
res.redirect('page_name');
}
else{
images.push(image);
req.session.msg='success';
res.redirect('page_name');
}
}
and this to get the page: 这得到页面:
app.get('/page_name', is_logged_in, function(req, res) {
if(req.session.msg){
res.render('page_name', {images: images, user:req.user, message: req.session.msg});
setTimeout(req.session.msg="", 4000);
}
else{
res.render('page_name', {images: images, user:req.user, message:''});
}
});
But I am still finding that the error message persists when the page is refreshed. 但是我仍然发现刷新页面后错误消息仍然存在。 Have I made a mistake? 我做错了吗?
currently you are doing this 目前您正在执行此操作
if (err){
res.render('page_name',{images:images, message:'There was an error'})
}
else{
images.push(image);
res.render('page_name',{images:images, message: 'Success'})}
}
you are getting url like this to 'my_site.com/add_new', which is correct rendering means your are loading/rendering html page to particular route which is '/add_new'. 您将这样的网址发送到“ my_site.com/add_new”, 这是正确的呈现方式, 这意味着您正在将html页面加载/呈现到特定的路由“ / add_new”。
There may be a two solution 可能有两个解决方案
1st solution 第一种解决方案
res.redirect('/?msg=' + string);
and on home page 和主页上
app.get('/', function(req, res){
if(req.query.msg){
// display error message
}else{
// else do not display error
}
});
2nd solution 第二解决方案
req.session.msg = "message here";
res.redirect('/');
and on home page you can do like this. 在首页上您可以这样做。
if(req.session.msg){
//display error message here
// after displaying msg in that last this
setTimeout(req.session.msg="", 3000); // this will clear a session after 3 seconds, so next time it will not display the msg.
} else {
//or display home page without error.
};
you can use one of them. 您可以使用其中之一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.