Ionic2 proxies not working with ion triggering but working with ion feed?

For my ionic.config.json , I have:

 { "name": "TSICMobile", "app_id": "6e4680fa", "typescript": true, "v2": true, "proxies": [ { "path": "/api", "proxyUrl": "http://192.168.0.105:8081/api" } ] } 

In my provider ( user-data.ts , based on the Ionic2 conference application) I have, for example:

 login(credentials) { return new Promise((resolve, reject) => { this.http.post( '/api/Login', JSON.stringify(credentials), { headers: this.contentHeader } ).subscribe(res => { console.log('api/Login return'); this.data = res.json(); if (this.data.authenticated === true) { this.storage.set('TSIC_USER_PROFILE', JSON.stringify(this.data.tsiC_USER_PROFILE)); this.storage.set('TSIC_USER_ROLES', JSON.stringify(this.data.listRoles)); this.storage.set('tsic_id_token', this.data.token); this.events.publish('user:login'); resolve(true); } else { reject('not authenticated'); } }, error => { console.log('api/Login failed'); reject(error); }); }); } 

at startup:

 ionic serve --lab -c 

the proxy works fine and fits into http://192.168.0.105:8081/api/Login

at startup

 ionic run android -c 

post url file://api/Login , and obviously not executed.

I need help in understanding why (apparently) the proxy server does not work when working on the device and what I can do wrong or do not understand.

+6
source share
3 answers

You do not need a proxy server when you are on your device, because Ionic can process corsers there. You need a proxy server because the browser is trying to handle CORS and its stricter ones with it.

I suggest you check if window.cordova exists and if it uses a regular URL, otherwise a proxy.

Like this:

  login(credentials) { return new Promise((resolve, reject) => { this.http.post( window.cordova?:'http://192.168.0.105:8081/api/Login':'/api/Login':, JSON.stringify(credentials), { headers: this.contentHeader } ).subscribe(res => { console.log('api/Login return'); this.data = res.json(); if (this.data.authenticated === true) { this.storage.set('TSIC_USER_PROFILE', JSON.stringify(this.data.tsiC_USER_PROFILE)); this.storage.set('TSIC_USER_ROLES', JSON.stringify(this.data.listRoles)); this.storage.set('tsic_id_token', this.data.token); this.events.publish('user:login'); resolve(true); } else { reject('not authenticated'); } }, error => { console.log('api/Login failed'); reject(error); }); }); } 
+1
source

The short answer is, proxies are really only useful for ion feed. For ion triggering you need to use cordova-plugin-whitelist

https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-whitelist/

What this means to you, you will have to change your URIs at build time. Therefore, instead of a simple / api / myAwesomeService, you will have http://192.168.0.105:8081/api as your URI when working on a real device.

0
source

This official article shows you exactly how to deal with this situation.

http://blog.ionic.io/handling-cors-issues-in-ionic/

an easier way is to define a constant like this:

 .constant('SERVER', { // when not using proxy //url: 'https://myextsite.com/api/public/index.php/v1' // when using proxy url: 'v1' }) 

ref: https://forum.ionicframework.com/t/solved-ionicview-app-http-request-to-external-api/18696/3

0
source

All Articles