CORS Issue


New to Ionic and created a basic app that grabs data from REST Api.

I’ve read the CORS section in the Ionic Pro documentation which lead me to believe that CORS was not an issue to worry about for package builds…is this correct?

I am able to pull the data successfully when testing from a browser by using a CORS-disabling add-on.

I am not able to pull the data using DevApp, which is what I expected.

I am not able to pull data after publishing an APK and installing on Android device. I expected this to work.

Any advice is appreciated.

Option 1: enable CORS on server side.

Option 2: use this library

You have to set something like this in your backend.

This is a example for nodejs witch expressjs:

app.use((req, res, next) => {
  res.setHeader('Access-Control-Allow-Origin', '*');
    'Origin, X-Requested-With, Content-Type, Accept, Authorization'

Thank you. I don’t control on the server side but I’ll try that library…looks like I’ll need to change from Http to HttpClient to get that library to work.

I made an attempt to change by importing HttpClient and changing the reference in the constructor but I’m getting back an error about parsing (looks like its attempting to parse the data as json). I did also put responseType: ‘text’ in the header…any tips?

Unfortunately I don’t control on the server side (I should have noted that) but thanks for the reply!

Yeah that library solves everything for you. Just set it up following the instructions in the repository and then just use Angular’s HttpClient as you would usually do