英文:
How to fix javascript not sending any req.body to server
问题
我没收到任何 req.body 参数。在 Postman 中使用 POST 请求时一切都正常。
代码:
async function postRequest(url, data){
const response = await axios.post(url, data, {
headers: {
"Content-Type": "application/json"
}
})
return response.data
}
服务器端:
app.use(bodyParser.urlencoded({extended: true}))
在添加任何 app.get
函数之前使用这个。
任何帮助将不胜感激。
FYI,我使用 ReactJS。
英文:
I m not getting any req.body parameters. Its working perfectly fine while posting with postman
Code:
async function postRequest(url, data){
const response = await axios.post(url, data, {
headers: {
"Content-Type": "application/json"
}
})
return response.data
}
server side:
app.use(bodyParser.urlencoded({extended: true}))
I use this before adding any app.get functions.
Any help would be appreciated.
FYI I use reactjs
答案1
得分: 0
你将内容类型设置为 JSON,但你的服务器期望的是 application/x-www-form-urlencoded
。你应该将内容类型设置为 application/x-www-form-urlencoded
。
async function postRequest(url, data) {
const response = await axios.post(url, data, {
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
});
return response.data;
}
英文:
You set the content type to json but your server is expecting application/x-www-form-urlencoded
. You should set the content type to application/x-www-form-urlencoded
async function postRequest(url, data){
const response = await axios.post(url, data, {
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
})
return response.data
}
答案2
得分: 0
尝试使用
app.use(bodyParser.json())
英文:
Try to use
'''
app.use(bodyParser.json())
'''
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论