React Native Expo fetch()错误网络请求失败

React Native Expo fetch() error Network request failed

我尝试在 React Native 中使用 fetsh() 从外部服务器获取数据。我的查询如下所示:

fetch('https://xxx.xx/login.php', {
  method: 'POST',
  headers: {
    Accept: 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    email: email,
    password: password
  })
}).then((response) => response.json())
  .then((responseJson) => {
   if(responseJson === 'Data Matched'){
     this.props.navigation.navigate('Second', { Email: email});
    }else{
     Alert.alert(responseJson);
    }
  }).catch((error) => {
    console.error(error);
    return error;
  });

在我的 Android 设备上执行期间,出现以下错误:

  Network request failed
  Stack trace:
  node_modules\whatwg-fetch\dist\fetch.umd.js:505:17 in setTimeout$argument_0
  node_modules\react-native\Libraries\Core\Timers\JSTimers.js:135:14 in _callTimer
  node_modules\react-native\Libraries\Core\Timers\JSTimers.js:387:16 in callTimers
  node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:425:19 in __callFunction
  node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:112:6 in __guard$argument_0
  node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:373:10 in __guard
  node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:111:4 in callFunctionReturnFlushedQueue
  [native code]:null in callFunctionReturnFlushedQueue
  ...

当我在浏览器中打开文件时,一切正常。

很高兴得到你的帮助。 非常感谢。 卢卡斯

经过长时间的搜索,我现在找到了解决方案。通过对我的证书 https://www.ssllabs.com/ssltest 的测试,我收到了以下消息:

This server's certificate chain is incomplete. Grade capped to B.

我将我的证书上传到 https://certificatechain.io/ 并将 .crt 文件添加到我的网络服务器上的 SSL 配置中。现在对我来说一切正常。

希望我能帮到你。