Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic2 proxies not working with ionic run but working with ionic serve?

Tags:

ionic2

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 Ionic2 conference app) 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);
        });
    });
}

when running:

ionic serve --lab -c

the proxy works perfectly and posts to http://192.168.0.105:8081/api/Login

when running

ionic run android -c

the post url is file://api/Login, and obviously fails.

Need assistance in understanding why (seemingly), the proxy is not in effect when running on device, and what I may be doing wrong or not understanding.

like image 410
Todd Greenwald Avatar asked Jul 13 '16 17:07

Todd Greenwald


1 Answers

You don't need a proxy when you are on your device because ionic can handle the cors there. You need the proxy on serve because the browser is trying to handle the CORS and its more strict with it.

What I suggest you do is check if window.cordova exists and if it does use the normal url and otherwise the proxy url.

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);
            });
        });
    }
like image 125
misha130 Avatar answered Nov 13 '22 05:11

misha130