响应本机fetch()网络请求失败

当我使用react-native init(RN版本0.29.1)创建一个全新的项目并将提取内容放入公共Facebook演示电影API的render方法中时,它将引发Network Request Failed。有一个非常无用的堆栈跟踪,我无法在chrome控制台中调试网络请求。这是我要发送的内容:


fetch('http://facebook.github.io/react-native/movies.json')

      .then((response) => response.json())

      .then((responseJson) => {

        return responseJson.movies;

      })

      .catch((error) => {

        console.error(error);

      });


慕码人8056858
浏览 794回答 3
3回答

侃侃尔雅

这里的问题是,iOS默认情况下不允许HTTP请求,仅允许HTTPS。如果要启用HTTP请求,请将其添加到您的info.plist:<key>NSAppTransportSecurity</key><dict>&nbsp; &nbsp; <key>NSAllowsArbitraryLoads</key>&nbsp; &nbsp; <true/></dict>

临摹微笑

React Native Docs给出了答案。Apple已阻止隐式明文HTTP资源加载。因此,我们需要添加以下项目的Info.plist(或等效文件)文件。<key>NSAppTransportSecurity</key><dict>&nbsp; &nbsp; <key>NSExceptionDomains</key>&nbsp; &nbsp; <dict>&nbsp; &nbsp; &nbsp; &nbsp; <key>localhost</key>&nbsp; &nbsp; &nbsp; &nbsp; <dict>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <true/>&nbsp; &nbsp; &nbsp; &nbsp; </dict>&nbsp; &nbsp; </dict></dict>React Native Docs->与现有应用程序集成->测试您的集成->添加应用程序传输安全例外
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript