当我使用react-native init (RN版本0.29.1)创建一个全新的项目,并在渲染方法中获取公共facebook演示电影API时,它抛出了一个网络请求失败。有一个非常无用的堆栈跟踪,我不能调试网络请求在chrome控制台。这是我发送的fetch:
fetch('http://facebook.github.io/react-native/movies.json')
.then((response) => response.json())
.then((responseJson) => {
return responseJson.movies;
})
.catch((error) => {
console.error(error);
});
通过在0.0.0.0上运行模拟服务器
注意:当你使用json-server运行另一个服务器时,这在Expo上有效。
另一种方法是在0.0.0.0而不是localhost或127.0.0.1上运行模拟服务器。
这使得模拟服务器可以在LAN上访问,因为Expo要求开发机器和运行Expo应用程序的移动设备在同一个网络上,所以模拟服务器也可以访问。
在使用json-server时,可以使用以下命令实现这一点
Json-server——host 0.0.0.0——port 8000 ./db。json,看
请访问此链接了解更多信息。
不建议允许http的所有域。
只对必要的域进行例外处理。
来源:在iOS 9和OSX 10.11中配置应用程序传输安全例外
将以下内容添加到信息中。你的应用的Plist文件:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>yourserver.com</key>
<dict>
<!--Include to allow subdomains-->
<key>NSIncludesSubdomains</key>
<true/>
<!--Include to allow HTTP requests-->
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<!--Include to specify minimum TLS version-->
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
</dict>
</dict>