【问题标题】:Network Error making post request using Axios使用 Axios 发出 post 请求时出现网络错误
【发布时间】:2020-10-26 03:35:03
【问题描述】:

我正在尝试让我的应用程序使用 Axios 发送发布请求并接收响应。但是,我在尝试发出发布请求时遇到了错误。

我的发帖请求代码:

onPostJson = () => {
  axios.post('https://10.1.127.17:11111/vpdu/get-ca-thu-hoi',
  {
    FromDate: "01-Jan-2020",
    ToDate: "01-Feb-2020",
    Ca: 1
  })
  .then((response) => {
    console.log(response.json());
  }, (error) => {
    console.log(error);
  });
};

错误:

Network Error
- node_modules\axios\lib\core\createError.js:15:17 in createError
- node_modules\axios\lib\adapters\xhr.js:80:22 in handleError
- node_modules\event-target-shim\dist\event-target-shim.js:818:39 in EventTarget.prototype.dispatchEvent
- node_modules\react-native\Libraries\Network\XMLHttpRequest.js:574:29 in setReadyState
- node_modules\react-native\Libraries\Network\XMLHttpRequest.js:388:25 in __didCompleteResponse
- node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:190:12 in emit
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:436:47 in __callFunction
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:111:26 in __guard$argument_0
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:384:10 in __guard
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:110:17 in __guard$argument_0
* [native code]:null in callFunctionReturnFlushedQueue

我怀疑网址有问题,但我使用 Postman 成功向该网址发出了 post 请求。

解决方案:是语法错误。我忘了在代码中包含 Header 配置。

onPostJson = () => {
      console.log("onpost");
      axios.post('http://10.1.127.17:11111/vpdu/get-ca-thu-hoi', {
        FromDate: "01-Jan-2020",
        ToDate: "01-May-2020",
      }, {
            headers: {
                'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1bmlxdWVfbmFtZSI6ImtpZW50ZC5haXRzIiwibmJmIjoxNTkzNzY0MDU0LCJleHAiOjE1OTQzNjg4NTQsImlhdCI6MTU5Mzc2NDA1NH0.liIM6g2E_EMXvnRpL1RcU-QVyUAKYxVLZZK05OqZ8Ck',
                'Content-Type': 'application/json',
                Accept: 'application/json',
            },
        })
        .then(respond => {
          // console.log(respond.data.CaThuHoiList);
          setShiftData(respond.data.CaThuHoiList);
        })
        .catch(function (error) {
          console.log('Error');
          console.log(error);
        });
    }

【问题讨论】:

    标签: json react-native post axios


    【解决方案1】:
    axios.post('https://10.1.127.17:11111/vpdu/get-ca-thu-hoi', {
            headers: {
                'Content-Type': 'application/json',
                Accept: 'application/json',
            },
              FromDate: "01-Jan-2020",
              ToDate: "01-Feb-2020",
              Ca: 1
        });
    

    我不确定,但是..

    你想试试上面的代码吗?

    【讨论】:

    • code 网络错误 - node_modules\axios\lib\core\createError.js:1:0 in - node_modules\axios\lib\adapters\xhr.js:73:25 in handleAbort * [native code]:null in callFunctionReturnFlushedQueue
    • onPostJson().then((getInfo) => { 你能用这个表格给我打电话吗?
    • 我只是想实现一个简单的 POST 请求功能,与您发送给我的链接相关的身份验证或主题无关。
    • 哦,对了。对不起!如果你看到这个链接后不能解决问题,你能再告诉我一次吗? stackoverflow.com/questions/53576923/…
    • 这很酷。我应该按照你说的做。我就是这样学会的。谢谢。
    猜你喜欢
    • 2020-03-06
    • 2021-06-07
    • 2021-03-26
    • 1970-01-01
    • 2019-08-22
    • 1970-01-01
    • 2020-04-03
    • 2020-06-07
    • 2018-08-14
    相关资源
    最近更新 更多