HTML表单未在发布请求中发送数据

时间:2019-12-04 19:07:41

标签: javascript html node.js

我正在尝试使用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

1 个答案:

答案 0 :(得分:1)

对于表单数据,您还需要使用: app.use(bodyParser.urlencoded({Extended:true})),然后您将在req.body中获得帖子数据