简体   繁体   中英

Ionic proxy works on ionic serve, not on ios simulator or device

I have a simple web service in my ionic app... it connects to remote server for simple tasks.

The proxy is setup in ionic like this:

  "proxies": [{
     "path": "/api",
     "proxyUrl": "http://example.com/api"
   }]

and the calls are to "/api". These resolve properly to http://example.com/api in localhost browser (using ionic serve).

But in xcode, the url is resolved to:

file:///api

I can't find anything on this specifically. How show I be calling for ios?

I faced the same issue while running on Android device, The solution is simple, point to note here is the proxy we are using to handle the CORS issue and this is the issue only in perticular with the desktop browser, so while using ionic serve we need proxy and we dont need to use proxy for other devices. I have used below condition in the service to handle this with out doing any further code changes while building for any target platform.

var APIUrl = '/myproxies';
if (this.platform.is('core') == true){
    APIUrl = '/myproxies';
}else{
    APIUrl = 'http://api.sample.com/SomeSampleAPIProvider';
}
this.http.get(APIUrl+"/Json").map(res => res.json()).subscribe(
    data => {.....

can refer this link to know usage details of this.platform

This is the intended action. The ionic CLI proxy is only used for ionic CLI serve or run commands. More here: http://blog.ionic.io/handling-cors-issues-in-ionic/

The file:///api url will not trigger the Angular browser CORS pre-flight OPTIONS call, so you should be fine.

Any additional CORS issues you are experiencing would be on the server side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM