通过Express.app中的app.post('/'呈现的页面上的``确认表单重新提交''问题 [英] `Confirm Form Resubmission` issue on page rendered thru app.post('/', in expressjs

查看:138
本文介绍了通过Express.app中的app.post('/'呈现的页面上的``确认表单重新提交''问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

html包含

form(name="Form", action="/", method="post")
  button(type='submit') submit

Express服务器包含:

Express server contains:

app.post('/', function(req, res){   
  res.render('home', {user: req.body});
});

如预期的那样,home.jade在浏览器中呈现.但是,当我在http://localhost:3000处刷新home.jade文件时,它要求确认重新提交表单,我想摆脱它-Confirm Form Resubmission

As expected the home.jade is rendered in the browser. But when I refresh the home.jade file at http://localhost:3000 It asks for Confirm Form Resubmission I want to get rid of this - Confirm Form Resubmission!

home.jade文件是包含简单文本的简单文件.

The home.jade file is simple file containing simple text.

推荐答案

问题并不是由您的代码引起的,而是由于浏览器希望在您收到POST的响应后再次发布信息.解决此问题的常用方法是重定向到同一页面.

The problem isn't really caused by your code, but by that browsers want to do a post again when you've received a response from a POST. A common way to solve this issue is to redirect to the same page.

app.post('/', function(req, res){   
  res.redirect('/');
});

现在,刷新时浏览器将改为获取GET'/'.

Now when you refresh the browser will refetch a GET '/' instead.

更新以反映更新的问题

要呈现发布在重定向页面上的数据,可以使用会话.

To render data that was posted on the redirected page, one can use sessions.

app.use(express.cookieSession());

app.post('/', function(req, res){
  req.session.user = req.body;
  res.redirect('/');
});

在用于GET'/'的中间件中,您必须确保使用该参数

And in the middleware for GET '/' you have to make sure to use that parameter

app.get('/', function(req, res){
  res.render('home', { user: req.session.user });
});

这篇关于通过Express.app中的app.post('/'呈现的页面上的``确认表单重新提交''问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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