CORS issue on real device


#1

Hello,
I’ve added proxy in ionic.config.json and also checked platform and based on my platform am changing API url but seems CORS issue is not fixed for me.

ionic.config.json

{
  "name": "eppm",
  "app_id": "",
  "type": "ionic-angular",
  "integrations": {
    "cordova": {}
  },
  "proxies": [
    {
      "path": "/path",
      "proxyUrl": "http://domain/path"
    }
  ]
}

Api-manager.ts

import { Platform } from 'ionic-angular';
@Injectable()
export class ApiManagerProvider {
url:string;
constructor(private platform: Platform, public http: HttpClient, private cManager: CookieManagerProvider) { }
 private makePostRequest(command: string, data: any) {

    if(this.platform.is('core') || this.platform.is('mobileweb')) {
      url = 'pace/rest/3.0/eppm/cmd/';
      console.log("Web");
    }
    else {
      url = 'http://xxx.xxxxxxx.com/pace/rest/3.0/eppm/cmd/';
      console.log("Mobile");
    }

    let apiHeader = new HttpHeaders();
    let contentType = apiHeader.set('Content-Type', 'application/x-www-form-urlencoded');
    let pace = contentType.append('pace-useragent', 'rest');

    let param = new URLSearchParams();
    for (var key in data) {
      param.append(key, data[key]);
    }

    return new Promise((resolve, reject) => {
      this.http.post(this.url + command, param.toString(), { headers: pace, withCredentials: true }).subscribe(res => {
        resolve(res);
      }, (error: HttpErrorResponse) => {
        reject(error);
      });
    });
  }
}

config.xml

<access origin="*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />

Note: I’m running this app on development mode.

Kindly help me to figure this out guys.
Thanks in advance.


#2

It is usually your API server that’s refusing your connection. What is your API server? PHP?


#3

@cedrickd APIs were build on Java platform.


#4

Hi @hardikajmeri Same problem here. Could you solve the issue?


#5

Hi,

You need to fix the issue in the backend, not in Ionic.


#6

Hello @pablocettour, I’ve came across with the following solutions:

  1. For Web Apps, user angular’s http client
  2. For Mobile App, use cordova plugin http plugin

Even after implementing above if you still faces any issues then you need to fix the same in backend.