【发布时间】:2017-05-12 15:53:47
【问题描述】:
我正在尝试使用 web 服务进行身份验证并进行后续的 api 调用。
export class HomeComponent {
newName: string;
headers = {
headers: new Headers({
'Content-Type': 'application/json',
'Accept': 'application/json'
})
};
constructor(public http: Http, public nameListService: NameListService) {
let _build = (<any>http)._backend._browserXHR.build;
(<any>http)._backend._browserXHR.build = () => {
let _xhr = _build();
_xhr.withCredentials = true;
return _xhr;
};
}
login(path, data) {
var creds = { 'Email': 'email@email.com', 'Password': 'pass', 'RememberMe': true };
this.http.post('http://xx.net/api/Users/Login', JSON.stringify(creds), this.headers)
.subscribe(res => {
console.log(res);
});
}
getdata() {
this.http.get('http://xx.net/api/Users/LoggedInUser', this.headers)
.subscribe(res => {
console.log(res);
}, (er) => {
console.log(er);
});
}
}
在构造函数中,我将 _xhr.credentails 设置为 true。 在 chrome 中一切正常,但在 IE 和 Safari 中失败。 当我尝试登录(在所有浏览器中)时,我可以看到带有“Set-Cookie”的响应标头,但是 safari 和 IE 似乎没有随后续请求发送此 cookie,所以我得到 401。Chrome 正确地做到了。
这是框架的错误吗?
【问题讨论】:
-
如果浏览器兼容性是问题,很可能是bug。我认为这个问题更适合用 plunker 解决 GitHub 问题。