Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【20161209】React Native Network Request Failed 解决方案 #59

Closed
zhongxia245 opened this issue Dec 9, 2016 · 1 comment
Closed

Comments

@zhongxia245
Copy link
Owner

React Native Network Request Failed

时间:2016-12-09 08:12:29
在ReactNative使用fetch去异步加载数据。 这里的 fetch 和浏览器的 fetch 用法是一样的。
但是, 这里的 异步请求,都不存在跨域问题。

一、问题描述

const url = 'http://bbs.reactnative.cn/api/category/3'
fetch(url)
  .then(res => res.json())
  .then(result => {
    console.log('result', result);
  })
  .catch(error => {
    console.log('error', error);
  })

Network Request Failed

二、原因

IOS9以上,默认需要请求https协议的接口导致该问题。

IOS9引入了新特性App Transport Security (ATS)。新特性要求App内访问的网络必须使用HTTPS协议,意思是Api接口以后必须是HTTPS。但是我的项目使用的是HTTP协议,现在也不能马上改成HTTPS协议传输。

三、解决方案

  1. 在Info.plist中添加NS App Transport Security类型Dictionary。
  2. 在NS App Transport Security下添加NSAllowsArbitraryLoads类型Boolean,值设为YES

设置完如图:

添加的时候,输入 NSAllowsArbitraryLoads 名称,显示的时候就如图一样。 NS 不显示了

四、请求到数据的效果图

@zhangwei900808
Copy link

我的是android也报这个错

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants