Axios POST 请求返回 Undefined
2020-07-06
1943
我正在尝试构建 react-native 函数以使用 Axios 向 API 发出 POST 请求。该函数似乎可以工作,但我一直收到未定义的响应。我已经使用 Postman 测试了 POST 请求,它正常工作。
{
"CaThuHoiList": [
{
"NGAY": "20-01-2020",
"TH_ID": 33,
"CA": 1,
"CT_DONVI_ID": 78,
"CREATE_BY": 4797,
"TRANG_THAI": "HT",
"CREATE_DATE": "20-01-2020 00:00",
"KHO_ID": null,
"LOAI": "TT"
},
{
"NGAY": "20-01-2020",
"TH_ID": 34,
"CA": 1,
"CT_DONVI_ID": 78,
"CREATE_BY": 4797,
"TRANG_THAI": "BD",
"CREATE_DATE": "20-01-2020 00:00",
"KHO_ID": null,
"LOAI": "TL"
}
],
"Status": 1,
"Message": ""
}
但是我在运行我的函数时无法获得相同的响应(未定义):
onPostJson = () => {
axios.post('https://10.1.127.17:11111/vpdu/get-ca-thu-hoi', {
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
body: JSON.stringify({
FromDate: "01-Jan-2020",
ToDate: "01-Feb-2020",
Ca: 1
})
})
.then(function (response) {
data = response;
console.log(data); \\Storing response in data still getting undefined
return response;
})
.catch(function (error) {
console.log(error);
});
}
有人能告诉我问题是什么吗?提前谢谢。
2个回答
您需要提取数据。我已完成更改。您可以查看。
onPostJson = () => {
let configObject = {
"url": "https://10.1.127.17:11111/vpdu/get-ca-thu-hoi",
"method": "post",
"headers": {
'Content-Type': 'application/json'
},
"data":{
"FromDate": "01-Jan-2020",
"ToDate": "01-Feb-2020",
"Ca": 1
}
}}
axios.request(configObject ).then((res) => {
console.log("react1: ", res);
console.log("react2: ", res.data);
})
}
Raghvender Kataria
2020-07-06
262778538
user17179431
2021-10-18