我很难理解如何在javascript中采用表单数据。例如:
firstName: req.body.firstName || null,
lastName: req.body.lastName || null
是来自html的firstName和lastName id用于标识数据来自哪个字段?
谢谢!
我们无法根据您发布的内容判断,如果它是直接形式的帖子(而不是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)
})
可能会进行某种形式的解析,将name
或其他识别字段转换为请求参数,例如
...url.../?firstName=bob&lastName=dobbs
来自http://expressjs.com/en/4x/api.html#req
req对象表示HTTP请求,并具有请求查询字符串,参数,正文,HTTP标头等的属性。在本文档中,按照惯例,该对象始终称为req(并且HTTP响应为res),但其实际名称由您正在使用的回调函数的参数确定。