首页 > 解决方案 > Cordova "Origin: file://"-Error using and Android 10, old versions working

问题描述

我做了一个 Cordova 应用程序,使用 ajax xml 将地理数据 (WFS) 发送到服务器 (geoserver)。服务器启用了 CORS 并设置了 CORS 源过滤器(浏览器版本需要此过滤器,此过滤器目前设置为 *,因此允许任何源)。POST 任务 (!!) 最多适用于 Android 9,使用 Android 10 我收到“错误 403”,因为 Android 10 在 POST 中发送 Origin:file://,这意味着 Origin“null”会导致此错误。

我已经阅读了几个小时的帖子并试图解决这个问题,但我无法做到。

任何人都可以帮助我 - 会非常好!

这是我的 Ajax-Post 的 js

transactWFS = function (mode, f) {
  var node;
  switch (mode) {
    case 'insert':
      node = formatWFS.writeTransaction([f], null, null, formatGML);
      break;
    case 'update':
      node = formatWFS.writeTransaction(null, [f], null, formatGML);
      break;
    case 'delete':
      node = formatWFS.writeTransaction(null, null, [f], formatGML);
      break;
  }
  var payload = xs.serializeToString(node);
  $.ajax('https://server.com/geoserver/database/ows', {
    type: 'POST',
    dataType: 'xml',
    processData: false,
    contentType: 'text/xml',
    data: payload,
  }).done(function () {
    //sourceWFS.clear();
  });
};

html-head 包含

<meta http-equiv="Content-Security-Policy" content="default-src *; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data:">

config.xml 包含

<plugin name="cordova-plugin-whitelist" source="npm" spec="~1.3.4" />
<access origin="*" />
<allow-navigation href="*"/>

标签: javascriptajaxcordovacorssame-origin-policy

解决方案


安装这个插件https://github.com/globules-io/cordova-plugin-ios-xhr

然后将您的偏好设置为

 <preference name="allowFileAccessFromFileURLs" value="true" />
 <preference name="allowUniversalAccessFromFileURLs" value="true" />
 <preference name="InterceptRemoteRequests" value="all" />

推荐阅读