【问题标题】:React Native 0.51.0 Fetch HTTPS network request failedReact Native 0.51.0 Fetch HTTPS 网络请求失败
【发布时间】:2018-01-07 18:09:02
【问题描述】:

我目前面临网络请求失败的错误。

这是我的代码:

fetch("https://nextbus.comfortdelgro.com.sg/eventservice.svc/BusStops")
  .then((response) => response.json())
  .then((responseJSON) => {
    console.log("JSON",responseJSON)
  })
  .catch((error) => {
    console.error(error);
  });

我正在构建一个 iOS 应用程序,我已经编辑了 info.plist 文件

<key>nextbus.comfortdelgro.com.sg/eventservice.svc/BusStops</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSTemporaryExceptionMinimumTLSVersion</key>
            <string>TLSv1.1</string>
        </dict>

我的网址是 HTTPS,所以我不明白为什么会遇到这个错误。

【问题讨论】:

    标签: ios react-native fetch react-native-ios


    【解决方案1】:

    我有同样的问题无法解决。我开始使用 Axios 库来发出请求。通过使用 Axios,您可以省去将 http 请求的结果传递给 JSON 方法的中间步骤。 Axios 只是返回你期望的数据对象

    【讨论】:

    • 使用 Axios 并不能解决问题,我正面临。不过感谢您的建议。
    【解决方案2】:

    您的代码没有任何问题。为了向自己证明这一点,只需将 url 换成 https://jsonplaceholder.typicode.com/posts/1 就可以了。

    此问题似乎是由响应的编码问题引起的。 当我在https://jsonformatter.curiousconcept.com/ 将 url 输入 JSON 格式化程序时,它会给出以下响应 - Invalid encoding, expecting UTF-8, UTF-16 or UTF-32.

    我对字符集知之甚少,无法提出解决此问题的建议,但希望这有助于从哪里开始寻找!

    【讨论】:

    • 您好,JSON 是有效的,我刚刚检查过。也许你复制错了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-04
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    • 1970-01-01
    相关资源
    最近更新 更多