【发布时间】:2020-12-27 16:18:10
【问题描述】:
我的前端代码:-
const [stocks, setStocks] = useState([
{
"name": "TSLA"
},
{
"name": "AAPL"
},
{
"name": "AMZN"
}
])
useEffect(() => {
fetch('http://localhost:2701/' , {
method: "POST",
headers: {
'Content-type': 'application/json'
},
body: JSON.stringify(stocks)
})
.then((result) => {
console.log(result)
})
.then((info) => { console.log(info); })
})
我的后端代码:-
router.post('/', async (req, res) => {
var data = req.body.data;
console.log("Data:-", data)
})
我想将值从 reactjs 发送到 nodejs 服务器,如果可能的话,任何人都可以建议使用 axios
【问题讨论】:
-
但是问题是什么?你有什么错误吗?
-
我得到未定义的值
-
你有没有解析body的中间件?是客户端发出了错误的请求,还是服务器错误地处理了它?找出并给相应方的minimal reproducible example。
-
你使用的是body-parser还是JSON parser?
-
这能回答你的问题吗? Fetch: POST json data