【问题标题】:Why req.body only get the name attribute from a POST request on node js?为什么 req.body 只从节点 js 上的 POST 请求中获取 name 属性?
【发布时间】:2019-02-16 10:48:39
【问题描述】:

这是我的第一个问题,如果是一个愚蠢的问题,我很抱歉。

我开始学习使用 javascript 和 node 进行 Web 开发,我的应用项目需要从 ejs 页面发送数据并存储在数据库中。我正在使用 req.body 但我不明白这个方法是如何工作的。

<form action="/exames" method="POST">
     <div class="form-group">
            <div class="custom-control custom-checkbox">
                <input type="checkbox" class="custom-control-input" id="customCheck[1]" name="exames[Hematologia]">
                <label class="custom-control-label" for="customCheck[1]">Hematologia</label>
            </div>
            <div class="custom-control custom-checkbox">
                <input type="checkbox" class="custom-control-input" id="customCheck[2]" name="exames[VHS]">
                <label class="custom-control-label" for="customCheck[2]">VHS</label>
            </div>
            <div class="custom-control custom-checkbox">
                <input type="checkbox" class="custom-control-input" id="customCheck[3]" name="exames[Coagulograma]">
                <label class="custom-control-label" for="customCheck[3]">Coagulograma</label>
            </div>
     </div>
</form>

在我的后端有代码:

var app = require("express")()
const bodyParser = require("body-parser");

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

app.post("/", function(req,res) {
    console.log(req.body);
}  

当所有三个复选框都被选中时,控制台中会显示一个对象{exames: {Hematologia, VHS, Coagulograma}}

我怎样才能在属性名称之外传递更多数据?

【问题讨论】:

  • 试着解释你希望通过什么。也许你想收到的一个例子。如果可以,请尝试说明您希望在什么条件下接收这些数据。

标签: javascript html node.js express ejs


【解决方案1】:

req.body 存储客户端发送的 POST 请求参数。 这里是link to the API

req.body.exames.hematologia
// => Hematologia
req.body.exames.vhs
// => VHS

要传递更多数据,您必须在表单中添加更多元素。并向我们​​提供有关您打算发布哪些数据的更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-28
    • 2019-05-13
    • 2017-08-19
    • 1970-01-01
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多