【问题标题】:PUT request not passing HTML form data as objectPUT 请求未将 HTML 表单数据作为对象传递
【发布时间】:2018-04-26 04:53:45
【问题描述】:

我无法通过 HTML 表单将对象传递到我的 PUT 路由。当我尝试 console.log 终端读取未定义的“产品”对象时。如果我不尝试将数据作为对象传递,我已经安装了 Method Override 并且一切正常。例如名称=“标题”和名称=“价格”。

app.js

router.put('/events/:id', function(req, res){
   console.log(req.body.product);
   res.redirect('/events/');
});

HTML

<form action="/events/<%= product._id %>?_method=PUT" method="POST">
  <div class="form-group">
    <input class="form-control" type="text" name="product[title]" value="<%= product.title%>">
  </div>                
  <div class="form-group">
    <input class="form-control" type="text" name="product[price]" value="<%= product.price%>">
  </div>
  <div class="form-group">
    <button>Submit</button>
  </div>
</form>  

【问题讨论】:

  • 哦!它被设置为假。现在工作。谢谢!

标签: html node.js express


【解决方案1】:

您的问题是没有正确配置body-parser,因为您可以访问您的中间件并从那里打印一些内容到您的控制台。只是为了确保你已经配置了这样的东西:

const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json({ type: 'application/json' }));

那么您的应用应该正确地将输入值填充到req.body

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-29
    • 1970-01-01
    • 1970-01-01
    • 2018-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多