对于我,ionic.config.json我有:
{
"name": "TSICMobile",
"app_id": "6e4680fa",
"typescript": true,
"v2": true,
"proxies": [
{
"path": "/api",
"proxyUrl": "http://192.168.0.105:8081/api"
}
]
}
Run Code Online (Sandbox Code Playgroud)
在我的提供商(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);
});
});
}
Run Code Online (Sandbox Code Playgroud)
在跑步时:
ionic serve --lab -c
Run Code Online (Sandbox Code Playgroud)
代理工作完美,并发布到http://192.168.0.105:8081/api/Login
跑步的时候
ionic run android -c
Run Code Online (Sandbox Code Playgroud)
邮政网址是file://api/Login,显然失败了.
需要帮助理解为什么(貌似),代理在设备上运行时无效,以及我可能做错了什么或不理解.
当您使用设备时,不需要代理,因为 ionic 可以处理那里的 cors。您需要服务器上的代理,因为浏览器正在尝试处理 CORS 并且对其更加严格。
我建议你检查 window.cordova 是否存在,以及它是否使用正常 url,否则使用代理 url。
像这样:
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);
});
});
}
Run Code Online (Sandbox Code Playgroud)