fetch() POST 对 Express.js 的请求生成空体 {}
fetch() POST request to Express.js generates empty body {}
目标: 在 fetch()
函数中从 HTML 发送一些已定义的字符串数据,例如"MY DATA"
我的代码:
HTML
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
function fetcher() {
fetch('/compute',
{
method: "POST",
body: "MY DATA",
headers: {
"Content-Type": "application/json"
}
}
)
.then(function(response) {
return response.json();
})
.then(function(myJson) {
console.log(myJson);
});
}
</script>
</body>
</html>
Server.js
var express = require("express");
var app = express();
var compute = require("./compute");
var bodyParser = require("body-parser");
//not sure what "extended: false" is for
app.use(bodyParser.urlencoded({ extended: false }));
app.post('/compute', (req, res, next) => {
console.log(req.body);
var result = compute.myfunction(req.body);
res.status(200).json(result);
});
当前: console.log(req.body)
日志 {}
期望: console.log(req.body)
日志 "MY DATA"
备注:
- 我也尝试在 fetch() 中发送正文作为
body: JSON.stringify({"Data": "MY DATA"})
但得到相同的空 {}
- 我的 fetch() 请求或 bodyParser() 设置不正确。
在您当前的 bodyParser app.use() 之前添加以下行:
app.use(bodyParser.json());
这将使 bodyParser 能够解析 application/json
的内容类型。
希望对您有所帮助!
当我弄清楚bodyParser.urlencoded
(来源:https://www.npmjs.com/package/body-parser#bodyparserurlencodedoptions)
后,我现在觉得有点傻
Returns middleware that only parses urlencoded bodies
解决方案
更改了 fetch()
请求,来自:
body: "MY DATA",
headers: { "Content-Type": "application/json" }
至
body: JSON.stringify({"Data": "MY DATA"}),
headers: { "Content-Type": "application/x-www-form-urlencoded" }
解决了这个问题!控制台记录:
{ '{"Data":"MY DATA"}': '' }
目标: 在 fetch()
函数中从 HTML 发送一些已定义的字符串数据,例如"MY DATA"
我的代码:
HTML
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
function fetcher() {
fetch('/compute',
{
method: "POST",
body: "MY DATA",
headers: {
"Content-Type": "application/json"
}
}
)
.then(function(response) {
return response.json();
})
.then(function(myJson) {
console.log(myJson);
});
}
</script>
</body>
</html>
Server.js
var express = require("express");
var app = express();
var compute = require("./compute");
var bodyParser = require("body-parser");
//not sure what "extended: false" is for
app.use(bodyParser.urlencoded({ extended: false }));
app.post('/compute', (req, res, next) => {
console.log(req.body);
var result = compute.myfunction(req.body);
res.status(200).json(result);
});
当前: console.log(req.body)
日志 {}
期望: console.log(req.body)
日志 "MY DATA"
备注:
- 我也尝试在 fetch() 中发送正文作为
body: JSON.stringify({"Data": "MY DATA"})
但得到相同的空 {} - 我的 fetch() 请求或 bodyParser() 设置不正确。
在您当前的 bodyParser app.use() 之前添加以下行:
app.use(bodyParser.json());
这将使 bodyParser 能够解析 application/json
的内容类型。
希望对您有所帮助!
当我弄清楚bodyParser.urlencoded
(来源:https://www.npmjs.com/package/body-parser#bodyparserurlencodedoptions)
Returns middleware that only parses urlencoded bodies
解决方案
更改了 fetch()
请求,来自:
body: "MY DATA",
headers: { "Content-Type": "application/json" }
至
body: JSON.stringify({"Data": "MY DATA"}),
headers: { "Content-Type": "application/x-www-form-urlencoded" }
解决了这个问题!控制台记录:
{ '{"Data":"MY DATA"}': '' }