CORS error in Ionic 3

2019-09-20 00:56发布

问题:

I am facing a very peculiar issue, when using Ionic 3 in iOS, I am trying to connect to a different domain [ddd.com], and since Ionic 3 uses WebKit, it changes the hostname to localhost:8080 in iOS.

This created a CORS issue, I was able to fix it by adding Allow-Control-Allow-Origin: http//localhost:8080 and Access-Control-Allow-Credentials: true.

Though this seems to work it had one issue, it never works the "first time" i.e when the app is downloaded and opened for the very first time. But it works when the app is killed and re-opened.

Any clue to un-wrap this tangly dangly mystery would be much appreciated.

  • Krishna Sagar

回答1:

Ionic3 use WKWebview where it enforces security for Ajax request. In 2 ways it can be rectified.

First solution: Use UIWebview instead of WKWebView. But these solution costs the performance of the App.

Second Solution Instead of angular HTTP call, Use native HTTP call. Use this plugin for easy implementation.

https://github.com/sneas/ionic-native-http-connection-backend