加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

React Native fetch网络请求失败iOS

发布时间:2020-12-15 20:31:28 所属栏目:百科 来源:网络整理
导读:我正在尝试使用fetch向Facebook Graph API发送GET请求: var url = "https://graph.facebook.com/v2.7/"+FACEBOOK_APP_ID+"?fields=context"+"{friends_using_app{id,name,picture.type(normal)}}"+"access_token="+_this.props.user.facebook_access_token;
我正在尝试使用fetch向Facebook Graph API发送GET请求:

var url = "https://graph.facebook.com/v2.7/"
+FACEBOOK_APP_ID
+"?fields=context"
+"{friends_using_app{id,name,picture.type(normal)}}"
+"&access_token="+_this.props.user.facebook_access_token;

fetch(url)
.then((response) => response.json())
.then((responseJson) => {
  console.log(responseJson);
})
.catch(function(error) {
  console.log(error);
});

但是我在JavaScript控制台中遇到TypeError:网络请求失败(…).这只发生在iOS上,它在Android上运行良好.

据我所知,iOS on react native默认允许HTTPS请求,因此不需要任何配置.

我可以使用fetch向https://google.com发出请求,当我打印url var并直接粘贴它时,我能够在safari中查看上述请求的结果.

似乎没有找到类似的东西,但如果这是重复的话,很抱歉.

解决方法

As far as I know,iOS on react native allows HTTPS requests by default,so this shouldn’t require any config.

这不太对.任何旧的https连接都不适合App Transport Security,因此您可能仍需要进行一些配置.这是the docs关于在没有配置的情况下满足ATS所需的确切内容:

With ATS fully enabled,your app’s HTTP connections must use HTTPS and
must satisfy the following security requirements:

The server certificate must meet at least one of the following trust
requirements: Issued by a certificate authority (CA) whose root
certificate is incorporated into the operating system Issued by a
trusted root CA and installed by the user or a system administrator
The negotiated Transport Layer Security version must be TLS 1.2 The
negotiated TLS connection cipher suite must support forward secrecy
(FS) and be one of the following:
TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384
TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA
TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256
TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA
TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA The leaf server certificate must be
signed with one of the following types of keys: Rivest-Shamir-Adleman
(RSA) key with a length of at least 2048 bits Elliptic-Curve
Cryptography (ECC) key with a size of at least 256 bits In addition,
the leaf server certificate hashing algorithm must be Secure Hash
Algorithm 2 (SHA-2) with a digest length of at least 256 (that is,
SHA-256 or greater). If ATS is not enabled,the system still performs
HTTPS server trust evaluation but you can override it on a
case-by-case basis,as described in HTTPS Server Trust Evaluation.
With ATS fully enabled,you cannot override the default HTTPS server
trust evaluation.

The requirements listed in this section are current as of this
document’s publication date,with stricter requirements possible in
the future. Changes to these requirements will not break app binary
compatibility.

我还创建了一个视频教程,介绍如何为您的本机应用程序发出网络请求:http://codecookbook.co/post/how-to-make-network-requests-in-react-native/

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读