首页 > 解决方案 > webview - access-control-allow-origin 不允许来源(使用cordova将角度转换为移动)

问题描述

我正在尝试使用 Alfresco adf 组件(https://github.com/Alfresco/alfresco-ng2-components)创建一个角度应用程序,并且也需要一个移动应用程序,所以我决定使用 cordova 生成 apk。

我按照以下教程(https://medium.com/@nacojohn/convert-your-angular-project-to-mobile-app-using-cordova-f0384a7711a6)生成我的apk。

一切似乎都很好,apk 已生成并安装在 BlueStack 中,直到我必须在我的应用程序中登录。

access-control-allow-origin 不允许来源

我知道我需要有一个白名单策略来导航应用程序 webview

配置文件

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.example.hello" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
  <name>HelloWorld</name>
  <description>
    A sample Apache Cordova application that responds to the deviceready event.
  </description>
  <author email="dev@cordova.apache.org" href="http://cordova.io">
    Apache Cordova Team
  </author>
  <content src="index.html" />
  <access origin="*" />
  <allow-intent href="http://*/*" />
  <allow-intent href="https://*/*" />
  <allow-intent href="tel:*" />
  <allow-intent href="sms:*" />
  <allow-intent href="mailto:*" />
  <allow-intent href="geo:*" />
  <allow-navigation href="*" />
  <platform name="android">
    <allow-intent href="market:*" />
  </platform>
  <platform name="ios">
    <allow-intent href="itms:*" />
    <allow-intent href="itms-apps:*" />
  </platform>
  <plugin name="cordova-plugin-device" spec="^2.0.2" />
  <plugin name="cordova-plugin-whitelist" spec="^1.3.3" />
  <engine name="android" spec="^7.0.0" />
  <engine name="ios" spec="^4.5.4" />
</widget>

索引.html

 <meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'"

我的 Angular Web 应用程序有一个开发服务器代理来解决 CORS 问题

proxy.conf.json

{
  "/alfresco": {
    "target": "server-url",
    "secure": false,
    "changeOrigin": true
  },
  "/activiti-app": {
    "target": "another-server-url",
    "secure": false,
    "changeOrigin": true
  }
}

我不知道我还需要做什么。我做错了什么吗?需要做更多的事情吗?

谢谢

标签: androidangularcordovacorsalfresco

解决方案


我认为您应该在 Alfresco 服务器中启用 CORS,您可以通过以下方式实现:

  • 在您的 Alfresco 扩展中包含此依赖项。
  • 或者通过直接在 Tomcat 服务器中启用 CORS,可以在Tomcat 文档中找到完整的详细信息。

推荐阅读