Skip to content

Accessing cdvfile:// from http://[network-url] in ajax throws CORS policy error on Android #352

Open
@vitto32

Description

@vitto32

While developing I run my app on an Android device. The app is served from a network url using cordova-plugin-webpack LiveReload (HMR) feature.

I've successfully implemented a download mechanism of JSON files and Audio files.
I can embed the downloaded audio file using cdvfile protocol (obtained through .toInternalURL) but I can not get the JSON files using Ajax requests because of this error:

Access to XMLHttpRequest at 'cdvfile://localhost/files/test/data.json' from origin 'http://10.123.123.123:8080' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, https.

Recap:

  • I can access APIs on the web via ajax.
  • I can download files using XHR and store them on the device.
  • I can embed the downloaded files through src='cdvfile://'
  • I can't ajax 'cdvfile://'

I have <access origin="*" /> and also tried <access origin="cdvfile://*" /> in config.xml
I have <allow-navigation href="cdvfile:*" />
CSP is set as follow:

default-src * cdvfile: data: blob: ws: wss: gap://ready file: http: https: 'unsafe-eval';
media-src * cdvfile:;
style-src * 'unsafe-inline';
script-src * cdvfile: 'unsafe-inline' 'unsafe-eval';
connect-src * ws: wss:;"

Any help would be appreciated

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions