[英]Html form not sending data in post request
我正在尝试使用 JWT 实现密码重置。 我正在向用户发送密码重置电子邮件,其中包含他的 ID 和参数中的 JWT。 当用户单击此链接时,它会将他重定向到密码输入页面。 此页面由以下函数呈现:
router.get('/resetPassword/:id/:token', function(req, res) {
if (req.params.token ==null || req.params.id == null) return res.send('Invalid reset
password link');
Organization.findById({_id: req.params.id}, function(err, org) {
if (err) return res.send(err);
if (!org) return res.status(HttpStatus.FORBIDDEN).send('No user associated with this
reset password link');
org.decodePasswordResetToken(req.params.token, function(err, decoded) {
if (err) return res.status(HttpStatus.INTERNAL_SERVER_ERROR).send(err);
console.log(`${req.params.id} ${req.params.token}`);
return res.send('<form action="/api/o/resetpassword" method="POST">' +
'<input type="hidden" id="id" name="id" value="' + req.params.id + '" />' +
'<input type="hidden" name="token" value="' + req.params.token + '" />' +
'<input type="password" name="password" value="" placeholder="Enter your new
password..." />' +
'<input type="submit" value="Reset Password" />' +
'</form>');
});
});
});
上面的代码包含我呈现给用户的表单。 现在的问题是当用户点击并提交表单时。 Form 中的值应提交给以下函数,但我没有在正文中获得任何值。 虽然我可以使用 GET 请求查看数据,但发布请求正文为空。
router.post('/resetpassword', function(req, res) {
const {id, token, password} = req.body;
Organization.findById({_id: id}, function(err, org) {
if (err) return res.send(err);
if (!org) return res.status(HttpStatus.FORBIDDEN).send('No user associated with this
reset password link');
org.decodePasswordResetToken(token, function(err, decoded) {
if (err) return res.status(HttpStatus.INTERNAL_SERVER_ERROR).send(err);
// set password for this user
org.password = password;
org.save(function(err, org) {
if (err) return res.send(err);
res.send('Your password has been successfully changed.');
});
});
});
});
我是 HTML 和 JS 的初学者。 对不起,如果这是一个愚蠢的错误。 :D
对于表单数据,你还需要使用: app.use(bodyParser.urlencoded({extended: true }))然后你会在req.body中得到post数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.