Ionic2代理不使用离子运行,但使用离子服务?

时间:2016-07-13 17:56:35

标签: ionic2

对于我的ionic.config.json我有:

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

在我的提供商(user-data.ts中,基于Ionic2会议应用)我有例如:

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);
        });
    });
}
跑步时

ionic serve --lab -c

代理完美运行并发布到http://192.168.0.105:8081/api/Login

运行时

ionic run android -c

帖子网址为file://api/Login,显然会失败。

需要帮助理解为什么(貌似),代理在设备上运行时无效,以及我可能做错了什么或不理解。

3 个答案:

答案 0 :(得分:1)

当您在设备上时,您不需要代理,因为离子可以处理那里的cors。你需要服务代理,因为浏览器正在尝试处理CORS,而且它更严格。

我建议你做的是检查window.cordova是否存在以及是否使用普通网址和其他代理网址。

像这样:

 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 :(得分:0)

简短回答是代理实际上只对离子服务有用。对于离子运行,您需要使用cordova-plugin-whitelist

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

这对您来说意味着,您必须在构建期间交换URI。因此,在真实设备上运行时,您实际上不会只使用/ api / myAwesomeService http://192.168.0.105:8081/api作为您的URI。

答案 2 :(得分:0)

这篇官方文章准确地向您展示了如何处理这种情况。

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

更简单的方法是定义一个常量,就像这样:

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

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

相关问题