请求出现错误:不允许到本地主机的明文 HTTP 流量

6 http ionic-framework capacitor

我正在使用电容器研究 ionic 4。我尝试从 localhost 调用 api,但收到此错误

There was an error with the request: Cleartext HTTP traffic to localhost not permitted!!

tob*_*ika 14

从 android 9 开始,他们试图推动人们使用 https API 而不是 http。但有一些方法可以禁用它。

AndroidManifest.xml简单的答案是通过修改并添加android:usesCleartextTraffic="true"到整个应用程序中允许 httpapplication

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:usesCleartextTraffic="true"
Run Code Online (Sandbox Code Playgroud)

对于更精细调整的域相关方法,我建议阅读以下内容: https://developer.android.com/training/articles/security-config#CleartextTrafficPermission