【问题标题】:Why i'm getting a network error, when trying to send a post request using axios为什么我在尝试使用 axios 发送发布请求时遇到网络错误
【发布时间】:2021-08-07 19:00:25
【问题描述】:

尝试使用 axios 发送发布请求时遇到网络错误。

错误: 网络错误 在 node_modules\axios\lib\core\createError.js:15:17 在 createError 在 node_modules\axios\lib\adapters\xhr.js:81:22 in handleError 在 EventTarget.prototype.dispatchEvent 中的 node_modules\event-target-shim\dist\event-target-shim.js:818:20 在 setReadyState 中的 node_modules\react-native\Libraries\Network\XMLHttpRequest.js:600:10 在 __didCompleteResponse 中的 node_modules\react-native\Libraries\Network\XMLHttpRequest.js:395:6 在 node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:189:10 在发出 在 __callFunction 中的 node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:416:4 在 __guard$argument_0 中的 node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:109:6 在 __guard 中的 node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10 在 node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:108:4 在 callFunctionReturnFlushedQueue at [native code]:null in callFunctionReturnFlushedQueue

代码:

const [state, setTest] = useState({data: null});
  const[isLoading, setLoading] = useState(true);
  const {testowy} = require("./clientRequests/Creq_lib");
  // testowy().then(data => setState({data: data})).catch(error => {console.log(error)}); This one doesn't work
  useEffect(() => {
    // axios.post("http://192.168.0.4:8080/testowy").then(res => {setTest({data: res.data}); setLoading(false);});  This works ok
    // testowy().then(res => {setTest({data: res}); setLoading(false);});
    testowy().then(res => {setTest({data: res}); setLoading(false);}); // These two don't work
  }, []);

当直接使用 axios.post() 时一切正常,但在我尝试导入一个函数后,它应该做完全相同的事情,我得到这个网络错误。

具有上述功能的库:

const {Creq_testowy} = require("./Creq_testowy");

module.exports={
    testowy: Creq_testowy,
}

函数本身:

const axios = require("axios");

function Creq_testowy() {
    return new Promise((resolve, reject) => {
        axios.post(`http://192.168.0.4:8080/testowy`)
            .then(res => {
                console.log(res.data);
                resolve(res.data);
            })
            .catch(error => {console.log(error)})
    });
}


module.exports = {
    Creq_testowy,
}

为什么直接使用 axios.post() 时一切正常? 为什么我在使用导入的函数时会出现这个错误?

【问题讨论】:

    标签: javascript react-native express axios


    【解决方案1】:

    为什么在 axios 已经返回一个时创建一个承诺? 你可以试试这个吗?

    async function Creq_testowy() {
      const res = await axios.post(`http://192.168.0.4:8080/testowy`)
      return res.data;
    }
    

    或者这个

    async function Creq_testowy() {
      try {
        const res = await axios.post(`http://192.168.0.4:8080/testowy`)
        return res.data;
      } catch(err) {
        console.log(err);
      }
    }
    
    

    您是否在本地运行此代码?世博会?网络?

    您是否还传递了为简洁起见隐藏的帖子参数? 这可能是该错误的根本原因。

    【讨论】:

    • 它现在可以工作了,但我仍然不知道为什么我的前一个会产生网络错误。谢谢,它是原生反应,我正在使用 expo、android studio 和 android 模拟器。
    • 补充:一段时间后我又回到了这个,我以前的灵魂也工作了,所以很奇怪。我已经重新启动了 Android Studio,现在它可以正常工作了。
    【解决方案2】:

    您可以尝试用以下代码替换您的代码吗:

    const axios = require("axios");
    
    async function Creq_testowy() {
        const data = await axios.post(`http://192.168.0.4:8080/testowy`)
        return data.data
    }
    
    module.exports = {
        Creq_testowy,
    }
    

    【讨论】:

    • 它现在可以工作了,但是为什么我以前的版本会产生这个错误?谢谢
    • 补充:一段时间后我又回到了这个,我以前的灵魂也工作了,所以很奇怪。我已经重新启动了 Android Studio,现在它可以正常工作了。
    猜你喜欢
    • 2018-08-14
    • 2020-03-06
    • 2022-11-10
    • 2018-06-21
    • 1970-01-01
    • 2018-12-30
    • 1970-01-01
    • 2020-10-26
    • 1970-01-01
    相关资源
    最近更新 更多