【问题标题】:TypeError: Network request failed androidTypeError:网络请求失败android
【发布时间】:2018-06-11 20:25:23
【问题描述】:

我在 RN 中有以下代码:

postToServer(){
  const requestBody = 'pin=1&status=false';
  return fetch('https://192.168.10.200/writeStatus.php', {
    method: 'POST',
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    body: requestBody
  }).then((response) => response.json())
        .then((responseJson) => {
          console.log(responseJson);
        });
}

我也有以下 php 文件:

<?php
  header('Access-Control-Allow-Origin: *');
  $data = json_decode(file_get_contents('php://input'), true);
  print_r($data);
?>

当我运行 postToServer 时,我收到:

可能的未处理的 Promise Rejection (id: 0): TypeError: Network 请求失败类型错误:网络请求失败 在 XMLHttpRequest.xhr.onerror (http://192.168.10.248:19001/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&hot=false&assetPlugin=P:\sandbox\MojDom1\node_modules\expo\tools\hashAssetFiles:7854:16) 在 XMLHttpRequest.dispatchEvent (http://192.168.10.248:19001/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&hot=false&assetPlugin=P:\sandbox\MojDom1\node_modules\expo\tools\hashAssetFiles:12942:35) ...(这里剩下一些行 - 我对链接有限制) 在 MessageQueue.callFunctionReturnFlushedQueue (http://192.168.10.248:19001/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&hot=false&assetPlugin=P:\sandbox\MojDom1\node_modules\expo\tools\hashAssetFiles:2122:12)

大家有什么建议吗?

【问题讨论】:

  • 您在哪个操作系统(android 或 ios)中或在模拟器或真实设备中遇到此问题?
  • 此调试器消息来自 Win10 上的 Chrome。我使用 exp 在我的 android 7.0 Huawei P10 上运行应用程序

标签: android reactjs react-native fetch typeerror


【解决方案1】:

为错误添加catch方法:

postToServer(){
  const requestBody = 'pin=1&status=false';
  return fetch('https://192.168.10.200/writeStatus.php', {
    method: 'POST',
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    body: requestBody
  }).then((response) => console.log(response))
}).catch(e => console.log(e));

【讨论】:

  • 捕获是输出此错误TypeError: Network request failed android
  • 我只是添加了一个catch 来正确处理错误。 TypeError: Network request failed android 问题可能来自服务器,因为请求无法连接成功。
  • 应用发送DELETE请求成功,服务器收到请求并处理,但.catch()仍然触发。这只发生在 android 模拟器和一个特定的 DELETE 请求上
  • 不太确定 react native,但可能需要权限?
  • 我不这么认为,因为另一个 DELETE 请求顺利通过并且 API 服务器没有抱怨。
猜你喜欢
  • 2022-01-12
  • 2020-04-09
  • 1970-01-01
  • 1970-01-01
  • 2018-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多