我可以从express检索(获取)数据,但不能将数据发布到express...
客户端:
<html>
<button onclick="myFunction()">send</button>
<script>
const data = {"experience" : 0};
function myFunction(){
fetch("/post", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(data)
})
}
</script>
</html>
快递:
这里我没有定义,但是当我添加express.json()时,我得到了"{}“。客户端和服务器的连接都很好,但是没有存储数据的主体吗?我通过将数据发布到webhooks.site来确保我的客户端代码工作得很好,它工作得很好。我觉得这是个菜鸟的错误...顺便说一句,我正在使用react和express,我只是简化了我的代码……如有任何建议,我们将不胜感激
const express = require("express");
const app = express();
app.post("/post", express.json() ,function (req,res){
console.log(req.body)
})
const port = 5000;
app.listen(port, () => `Server running on port ${port}`);
发布于 2021-09-01 16:55:42
我不得不使用Body Parser (简单)或Busboy (高级)来使用Express访问POST正文中的数据。
示例可能如下所示:
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
app.post("/post",(req,res,next)=>{
console.log(req.body);
})
https://stackoverflow.com/questions/69015796
复制相似问题