Joe Scotto
Joe Scotto

Reputation: 10857

Not able to bypass CORS in Ionic 4 application

I'm attempting to control my GoPro Fusion using this GitHub project. I'm able to control it whenever I run a POST request, but now I am trying to listen for changes in status on http://10.5.5.9/gp/gpControl/status by running a GET request.

Every time I request this URL I get the following in my safari inspector:

XMLHttpRequest cannot load http://10.5.5.9/gp/gpControl/status due to access control checks. Error: Uncaught (in promise): Response with status: 0 for URL: null Failed to load resource: Origin ionic://localhost is not allowed by Access-Control-Allow-Origin.

I have been running my app with ionic cordova run ios -l -c and tried setting up a proxy in ionic.config.json with:

"proxies": [
  {
    "path": "/gp/gpControl",
    "proxyUrl": "http://10.5.5.9"
  }
]

but this did not work, I instead thought that since the proxy did not work that I should attempt just installing it without the live reload server and I still get the exact same error.

If I go to the url in my mobile browser it loads the json just fine, it's only when I attempt to run a GET on it, that it fails and hits me with 2 cors errors.

My code for actually running the GET looks like this:

constructor(
  private http: Http
) {
  setInterval(() => {
    this.get('http://10.5.5.9/gp/gpControl/status').then(a => {
      console.log(a);
    });
  }, 1500);
}

public get(path, params = {}): Promise<any> {
  return this.http.get(this.baseUrl + path, params).toPromise();
}

I've been pulling my hair out with this issue for over 12 hours now and have absolutely no idea what to try next so any help at all would be very much appreciated.

Upvotes: 0

Views: 1366

Answers (1)

Joe Scotto
Joe Scotto

Reputation: 10857

Solved by simply using cordova-plugin-advanced-http

After testing with AngularFire on a standard Swift project and it working without any issues, I installed the above plugin and all requests are working as expected.

Upvotes: 2

Related Questions