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

使用fetch访问http网页报TypeError: Network request failed错误 #99

Closed
ACDC5 opened this issue Apr 1, 2020 · 7 comments
Closed

Comments

@ACDC5
Copy link

ACDC5 commented Apr 1, 2020

我使用expo构建项目,
android虚拟机版本10,
文档中提到了
**默认情况下,iOS 会阻止所有 http 的请求,以督促开发者使用 https。如果你仍然需要使用 http 协议,那么首先需要添加一个 App Transport Security 的例外,详细可参考这篇帖子。

从 Android9 开始,也会默认阻止 http 请求,请参考相关配置**
我根据官方提供的文档https://blog.csdn.net/qq_40347548/article/details/86766932想对android进行配置,但在expo的目录下没发现相关的文件夹(如下图)
image请问我该如何配置android端。

这是我的代码:
image

报错信息:
image

@sunnylqm
Copy link

sunnylqm commented Apr 1, 2020

expo的麻烦就在于你不能更改原生配置,只能等官方发布新版本
看起来官方是在sdk33中修复了 expo/expo@fdc6a3a

@ACDC5
Copy link
Author

ACDC5 commented Apr 1, 2020

鱼和熊掌不可兼得?看来expo还不是很完美,还是用react-native init吧

@ACDC5 ACDC5 closed this as completed Apr 1, 2020
@ACDC5 ACDC5 reopened this Apr 3, 2020
@ACDC5
Copy link
Author

ACDC5 commented Apr 3, 2020

记录一下,新建一个reactNative项目后将参数格式改为:
image
即key=value的形式后请求成功。没有对android目录内容进行任何配置。

网上解决这个问题的其他方向有:
1、虚拟机的dns要和本机的dns保持一致;
2、需要配置相应平台的网络设置,如ios对http的限制,android也存在类似的问题

@ACDC5 ACDC5 closed this as completed Apr 3, 2020
@sunnylqm
Copy link

sunnylqm commented Apr 3, 2020

  1. content-type任何形式,与http协议、系统本身对http协议的限制没有任何关系,不可能解决其相关的问题。好比你打电话的音量大小,不可能影响你的话费多少。
  2. content-type具体用什么格式,由服务器决定,它要json格式要xml格式要kv格式,你都得听它的。

@ACDC5
Copy link
Author

ACDC5 commented Apr 4, 2020

我认为问题出在传参上,Content-Type: application/x-www-form-urlencoded这种格式要求参数以key=value的方式传入;我一开始用的FormData也是k-v格式,正常来说我这样传参是没问题的,但还是出错了。我找到的解释是:FormData.append方法只能在Web Workers 中使用:
image
就是因为这个出现了TypeError: Network request failed错误。

@sunnylqm
Copy link

sunnylqm commented Apr 4, 2020

你的理解还是错误的,
1、form data对应的是multipart/form-data,而不是application/x-www-form-urlencoded
2、form data在web workers中可用,当然不是“只能”。如果是这样,谁还用form data?

@ACDC5
Copy link
Author

ACDC5 commented Apr 7, 2020

明白了 谢谢

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