gpt4 book ai didi

javascript - 理解 JavaScript 中的 body 参数

转载 作者:行者123 更新时间:2023-11-28 17:36:12 24 4
gpt4 key购买 nike

我很难理解 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)
})

关于javascript - 理解 JavaScript 中的 body 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49081012/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com