【问题标题】:'file://' has been blocked by CORS policy'file://' 已被 CORS 策略阻止
【发布时间】:2019-08-07 22:01:01
【问题描述】:

我在我的应用程序中使用 ionic3。我第一次安装 android 应用程序时遇到错误。然后在重新启动应用程序后正常工作。

从源 'file://' 访问 '' 处的 XMLHttpRequest 被 CORS 策略阻止:对预检请求的响应不 通过访问控制检查:没有“Access-Control-Allow-Origin”标头 出现在请求的资源上。

软件规格是:

cordova 9.0.0
ionic 3
Android platform 8.0.0
Node 10.15.3
Angular 4

插件列表:

cordova-plugin-camera 4.0.3 "Camera"
cordova-plugin-compat 1.2.0 "Compat"
cordova-plugin-device 2.0.2 "Device"
cordova-plugin-firebase-lib 3.3.0 "Google Firebase Plugin"
cordova-plugin-geolocation 2.4.3 "Geolocation"
cordova-plugin-inappbrowser 3.0.0 "InAppBrowser"
cordova-plugin-insomnia 4.3.0 "Insomnia (prevent screen sleep)"
cordova-plugin-ionic-keyboard 2.1.2 "cordova-plugin-ionic-keyboard"
cordova-plugin-ionic-webview 1.1.19 "cordova-plugin-ionic-webview"
cordova-plugin-market 1.2.0 "Market"
cordova-plugin-network-information 2.0.1 "Network Information"
cordova-plugin-screen-orientation 3.0.1 "Screen Orientation"
cordova-plugin-splashscreen 5.0.2 "Splashscreen"
cordova-plugin-statusbar 2.4.2 "StatusBar"
cordova-plugin-whitelist 1.3.3 "Whitelist"
cordova.plugins.diagnostic 4.0.8 "Diagnostic"
es6-promise-plugin 4.2.2 "Promise"
info.protonet.imageresizer 0.1.1 "Image Resizer"

已尝试的解决方案:

  1. 白名单插件仅限于 1.3.3
  2. 添加到以下行

    <allow-intent href="file://*" />
    <allow-intent href="file://" />
    <allow-navigation href="file://" />
    <allow-navigation href="file://*" />
    <allow-navigation href="*" />
    
  3. 也检查了升级 webview 和白名单插件。 没有任何效果。

如有需要,请提出更改建议。即使在生产版本中,我们也没有发布新版本。旧的 bbuild 遇到了这个问题

【问题讨论】:

  • 这是调用api的时候吗?
  • 是的,尼丁。当我调用我的后端 API 时。我开始收到此错误。我找到了它的根本原因。期待 tl 的修复。感谢您调查问题。 :)
  • 大家好,Google 已经发布了 chrome 的新更新,并且该应用程序按预期运行。请更新您的 chrome 并检查一次。 :)
  • @Priya 我也遇到了同样的问题,你找到解决办法了吗?
  • 嗨Pankaj,它与Webview有关吗?

标签: cordova ionic-framework


【解决方案1】:

我找到了答案。新更新的 chrome 版本出现此问题,并且在移动设备中的旧版本 chrome 上运行良好。

【讨论】:

    【解决方案2】:

    我有同样的问题,我正在从 Android 9 上的 cordova 6.5.0 应用程序调用后端 API。

    在从 Play 商店安装 Chrome 更新(v76.0.3809.89 - 7 月 30 日)之前,一切正常。现在,当我第一次安装应用程序或清除应用程序数据时,我总是会遇到 CORS 问题。

    如果我启动应用程序然后将其终止,则在重新启动后它又会恢复正常。我觉得这有点奇怪。

    【讨论】:

      【解决方案3】:

      猜测它必须与 chrome 更新有关,以及从 Android v7 开始,android 如何通过 chrome APK 而不是系统 webview 使用 webview。

      所以...更新到最新版本的 chrome 会影响 webview,因此您的 ionic 应用程序(使用 webview 进行渲染)崩溃..

      发现此链接有帮助 https://www.androidpolice.com/2016/07/20/google-explains-chrome-will-become-webview-android-7-0/

      【讨论】:

      • 谢谢先生。如果您有任何解决方案,请告诉我。
      【解决方案4】:

      您可以通过下面的插件将 Angular HTTP 请求代理到 Native HTTP (cordova HTTP) 来解决这个问题。通过使用本机 HTTP,您将摆脱 CORS 问题。

      https://github.com/sneas/ionic-native-http-connection-backend

      基本上,您将使用相同的本机实现替换有角度的 HttpBackEnd 接口。 (AppModule 中的提供者)

      providers: [
              {provide: HttpBackend, useClass: NativeHttpFallback, deps: [Platform, NativeHttpBackend, HttpXhrBackend]},
      ]
      

      【讨论】:

        【解决方案5】:

        如果您尝试访问移动存储文件,那么您应该使用 httpd 插件创建本地服务器。

        https://stackoverflow.com/a/57494421/7456041

        【讨论】:

          猜你喜欢
          • 2021-01-07
          • 1970-01-01
          • 2021-09-05
          • 2021-10-13
          • 2019-11-01
          • 2023-04-10
          • 1970-01-01
          • 2020-10-31
          • 2021-10-27
          相关资源
          最近更新 更多