Axios POST请求返回未定义

时间:2020-07-06 08:32:31

标签: javascript react-native post axios undefined

我正在尝试构建本机功能以使用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);
    });
}

谁能告诉我这是什么问题?预先感谢。

1 个答案:

答案 0 :(得分:1)

您需要提取数据。我已完成更改。您可以签出。

     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);
          
      })
}