【问题标题】:React Native Fetch Remote Http request throws Network Request Failed (On Android Device)React Native Fetch Remote Http 请求抛出网络请求失败(在 Android 设备上)
【发布时间】:2020-07-05 16:31:21
【问题描述】:

我正在尝试访问 本地计算机上的远程 HTTP Api。该 API 在我本地机器上的 Postman 和 Chrome 上运行良好。但是当使用 Expo React Native Fetch 进行 Fetch 调用时,我收到以下错误:

网络请求失败

node_modules\whatwg-fetch\dist\fetch.umd.js:473:29 in xhr.onerror 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 在 setReadyState
node_modules\react-native\Libraries\Network\XMLHttpRequest.js:388:25 在 __didCompleteResponse
node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:190:12 在发射
node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:436:47 在 __callFunction
node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:111:26 在 __guard$argument_0
node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:384:10 在 __guard
node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:110:17 在 __guard$argument_0
[本机代码]:null in callFunctionReturnFlushedQueue**

为了您的信息,我正在尝试使用 expo 客户端在最新的 Android 设备上运行该应用程序。

代码:

componentDidMount() {
  const url = (Remote Http URL);
  this.setState({
    loading: true
  });
  fetch(url)
    .then(res => res.json())
    .then(res => {
      this.setState({
        loading: false,
        error: res.error || null,
        data1: res
      }, () => {
        console.log(res);
      });
    })
    .catch(error => {
      this.setState({
        error,
        loading: false
      }, () => {
        console.log(error);
      });
    });
}

请帮我解决这个问题。谢谢。

【问题讨论】:

    标签: javascript android node.js react-native


    【解决方案1】:

    我发现这个问题的唯一解决方案是将要获取的 URL 设置为我的 IP 地址作为主机(因为我的手机 React Native 的运行方式与后端不同)。最后将后端设置为 0.0.0.0:8080 似乎已经解决了这个问题。至少暂时。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-16
      • 2020-07-04
      • 1970-01-01
      • 2017-06-05
      • 2020-06-23
      • 1970-01-01
      相关资源
      最近更新 更多