简体   繁体   English

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

[英]Html form not sending data in post request

I am trying to implement password reset using JWT.我正在尝试使用 JWT 实现密码重置。 I am sending a password reset email to user with his ID and JWT in params.我正在向用户发送密码重置电子邮件,其中包含他的 ID 和参数中的 JWT。 When user clicks this link, it redirects him to a password input page.当用户单击此链接时,它会将他重定向到密码输入页面。 This page is rendered by following function:此页面由以下函数呈现:

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>');
    });
  });
});

Above code contains the form that I am rendering to user.上面的代码包含我呈现给用户的表单。 Now the problem is when user clicks and submit the form.现在的问题是当用户点击并提交表单时。 The values from Form should be submitted to the following function but I am not getting any values in the body. Form 中的值应提交给以下函数,但我没有在正文中获得任何值。 Although I can see the data using GET request but post request body is empty.虽然我可以使用 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.');
        });
      });
   });
 });

I am a beginner in HTML and JS.我是 HTML 和 JS 的初学者。 Sorry if this is a stupid mistake.对不起,如果这是一个愚蠢的错误。 :D :D

对于表单数据,你还需要使用: app.use(bodyParser.urlencoded({extended: true }))然后你会在req.body中得到post数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM