我正在使用Ionic和Angular开发应用程序,我发出了一个发布请求,该请求向我返回了Authorization标头的令牌,直到那时我就可以使用以下代码正常获取此值:
data.headers.get('Authorization')
完整代码:
logIn(username, password) {
this.body = {
'username': username,
'password': password
};
this.apiProvider.getToken(this.body, this.headers).subscribe(
data=>{
this.storage.set('token', data.headers.get('Authorization'));
this.navCtrl.setRoot(SideMenuPage);
}, error=>{
console.log(error);
this.msgError = true;
}
);
}
问题是,仅当我在浏览器中运行该应用程序时,它才起作用;当我尝试在android模拟器上运行时,它返回null。
似乎没有CORS问题。
我的后端已经有
...
response.addHeader("access-control-expose-headers", "Authorization");
...
config.xml
<access origin="*" />
<allow-navigation href="http://tstapi.apco.petrobras.com.br/*" />
<allow-navigation href="data:*" />
<allow-intent href="http://tstapi.apco.petrobras.com.br/*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
重要的是,请求成功结束,但是数据没有返回。
<allow-intent href="http://*"/>
<allow-intent href="https://*"/>