我在nativescript-vue中使用fetch时遇到问题

问题描述 投票:0回答:1

当我向 ("http://localhost:5000/api") 发出获取请求时 我收到此错误:

[类型错误:网络请求失败:无法加载资源,因为应用程序传输安全策略要求使用安全连接。]

这是我用来尝试获取数据的代码,但我总是收到该错误。解决此问题的最佳方法是什么,或者是否有其他解决方案可用于在 Nativescript 中获取和发布数据?

methods: {
      getData() {
       fetch("http://localhost:5000/api")
          .then((res) => {
            console.log(res)
       }).catch((err) => {
     console.log(err)
   });

 },
nativescript fetch-api nativescript-vue
1个回答
2
投票

iOS 和 Android(v9.0 及以上版本)中默认不启用 Http。

要在 iOS 上启用 Http 请求,您必须将以下键添加到您的

App_Resources/iOS/info.plist

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSAllowsArbitraryLoads</key>
  <true/>
</dict>

在 Android 上,您必须将

android:usesCleartextTraffic="true"
添加到
application
App_Resources/Android/src/main/AndroidManifest.xml

标签中
<application android:name="com.tns.NativeScriptApplication" 
        ...
        android:usesCleartextTraffic="true">

您也可以仅在特定域上允许 Http 通信,请参阅官方 iOS / Android 文档以获取更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.