【问题标题】:Body Parser not reading data from formBody Parser 不从表单中读取数据
【发布时间】:2017-12-19 17:31:09
【问题描述】:

我似乎无法弄清楚我到底做错了什么。我正在使用 GET 请求呈现表单,并尝试将信息解析为 POST 请求并将其呈现为 JSON。

app.get('/search', (req, res) => {
  res.render('searchForm')
})

app.post('/results', (req, res) => {
  res.json(req.body);
})

这就是我的表单的样子(它是一个 pug/jade 文件):

extends layout

block content

  div.container  
    h1 This is the events page
    form(action="/results" method="POST")
      label(for="address") Search!
      br
      input(type="text" name="address" placeholder="Address")
      br
      br
      input(type="submit" value="Search   ????" class="btn btn-primary")

当 console.logging req.body 到服务器时,我得到 undefined 而当我将它发送到客户端时,我什么也得不到,控制台上也没有任何显示。有什么问题?

【问题讨论】:

    标签: javascript json express pug body-parser


    【解决方案1】:

    使用模块时出现错别字:

    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: true }));
    

    【讨论】:

      猜你喜欢
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多