【问题标题】:Unable to connect to server with Cordova-app on device无法使用设备上的 Cordova-app 连接到服务器
【发布时间】:2015-11-27 12:54:05
【问题描述】:

我用 Cordova 创建了一个 Android 应用程序,在模拟器上它就像一个魅力。当我尝试在设备上运行它时,我没有通过登录屏幕,因为我收到一条错误消息:“Access-Control-Allow-Origin 不允许来源”。这是 Cordova 中的 CSP 问题还是我的服务器有问题? 一些Config.xml:

  <access origin="*" />
  <access origin="baseUrlToServer.com" />
  <allow-intent href="http://*/*" />
  <allow-intent href="https://*/*" />
  <allow-intent href="tel:*" />
  <allow-intent href="sms:*" />
  <allow-intent href="mailto:*" />
  <allow-intent href="geo:*" />

还有我在 Index.html 中的 CSP 元标记:

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

有没有人偶然发现类似的东西并有想法?非常感谢您的帮助:)

【问题讨论】:

    标签: android cordova server


    【解决方案1】:

    问题出在服务器端。

    您的服务器必须回复一些HTTP RESPONSE HEADERS,其中:

    Access-Control-Allow-Origin: *
    

    以 apache2 为例

    <IfModule mod_headers.c>
      Header add Access-Control-Allow-Origin "*"
      Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
      Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
    </IfModule>
    

    【讨论】:

    • 这是通过服务器上的 CORS 覆盖的。
    • 好的,但是如果您收到Origin is not allowed by Access-Control-Allow-Origin 错误,那就有问题了。
    • 好的,但是在我的服务器上,我已经在 ASP.NET 5 中启用了AllowAnyHeader(); AllowAnyMethod() AllowAnyOrigin() AllowCredentials()。还有什么其他的事情我应该去看看?仍然有这个问题..
    • 不,重要的是这些设置。也许在某个地方,其他配置主题会覆盖这些主题。例如,您的应用程序前面是否有一个 Web 服务器,例如 nginx 或类似的东西?我认为你应该朝这个方向搜索。
    猜你喜欢
    • 1970-01-01
    • 2018-07-12
    • 2013-03-04
    • 2016-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-15
    相关资源
    最近更新 更多