在javascript中理解身体参数

问题描述 投票:0回答:2

我很难理解如何在javascript中采用表单数据。例如:

firstName: req.body.firstName || null,
                lastName: req.body.lastName || null

是来自html的firstName和lastName id用​​于标识数据来自哪个字段?

谢谢!

javascript express
2个回答
2
投票

我们无法根据您发布的内容判断,如果它是直接形式的帖子(而不是AJAX),那么数据将来自input / select元素,这些元素具有相应的名称,例如

<form method="POST" action="/express/endpoint">
   <input type="text" name="firstName" />
   <input type="text" name="lastName" />
   <input type="submit" />
</form>

这也可以通过AJAX手动发送:

fetch('/express/endpoint', {
    body: JSON.stringify({ firstName: 'foo', lastName: 'bar' }),
    headers: {
      'content-type': 'application/json'
    },
    method: 'POST'
}).then(function(response) {
  console.log(response)
})

1
投票

可能会进行某种形式的解析,将name或其他识别字段转换为请求参数,例如

...url.../?firstName=bob&lastName=dobbs

来自http://expressjs.com/en/4x/api.html#req

req对象表示HTTP请求,并具有请求查询字符串,参数,正文,HTTP标头等的属性。在本文档中,按照惯例,该对象始终称为req(并且HTTP响应为res),但其实际名称由您正在使用的回调函数的参数确定。

© www.soinside.com 2019 - 2024. All rights reserved.