[英]Angular http stream double request [GET,HEAD]
I am working on learning Angular[6], and while I don't have a very strong understanding of Observables, I feel like I've at least built a usable example. 我正在学习Angular [6],虽然我对Observables不太了解,但我觉得至少已经建立了一个可用的示例。 However, I am running into a consistent problem which leads me to believe that I am using a poor approach or have fundamentally misunderstood something.
但是,我遇到了一个始终如一的问题,这使我相信我使用的方法不佳或从根本上误解了某些内容。
In my ApiService: 在我的ApiService中:
getTransaction(uid:number):Observable<Transaction[]>{
let url = this.endPoint + '/transaction';
if(uid !== undefined){
url += '/' + uid;
}
return this._httpRequest('GET',url,this._buildAuthHeader(),null).map((response)=>{
if(response['body'] && uid === undefined){
return <any>response['body'].map((transaction)=>{return new Transaction(transaction);});
}else if(response['body']){
return new Transaction(response.body);
}
});
}
And then it's implementation in my BuddyService: 然后在我的BuddyService中实现:
constructor(private ApiService:ApiService,private cookie:CookieService) {
this.checkCookie();
}
authenticate(username,password):void{
this.ApiService.authenticate(username,password).subscribe((response)=>{
if(!response['error']){
this.ApiService.token = response.token;
this.cookie.set('auth_token',this.ApiService.token);
this.checkCookie();
}else{
console.log(response);
}
});
}
checkCookie():void{
if(this.cookie.check('auth_token')){
this.ApiService.token = this.cookie.get('auth_token');
this.ApiService.verifyToken().subscribe((response)=>{
if(!response['error']){
this.init();
}else{
// console.log(response);
}
});
}
}
init():void{
this.ApiService.getTransaction(undefined).subscribe((data)=>{console.log(data);this.transactions = data;});
}
When this is served and the page is loaded, everything functions as expected. 提供此服务并加载页面后,所有功能都会按预期运行。 Except that each call to the API is actually 2 calls, and OPTIONS request, followed by my real request causing in the example above this.transactions to always be undefined before it is ever filled.
除了对API的每次调用实际上是2次调用,然后是OPTIONS请求,然后是我的实际请求,在上面的示例中导致this.transactions在填充之前始终是未定义的。 What am I doing wrong here?
我在这里做错了什么? How can I subscribe to the result of the real request and not the OPTIONS request?
如何订阅真实请求而不是OPTIONS请求的结果?
Thansk in advance! 提前谢谢!
EDIT: 编辑:
getTransaction(uid:number):Observable<Transaction[]>{
let url = this.endPoint + '/transaction';
if(uid !== undefined){
url += '/' + uid;
}
let req = new HttpRequest(
'GET',
url,
{
headers:new HttpHeaders({'auth_token':this.token});
}
);
return this.http.request(req).map((response)=>{
console.log(response);
});
}
After slimming down to a more barebones version of my problem I am still seeing the same results. 在缩小到我的问题的更准系统之后,我仍然看到相同的结果。
console.log(response);
will first render {type:0}
on the preflight and an HttpResponse
on the true request. 将首先在预检时呈现
{type:0}
,并在真实请求时呈现HttpResponse
。
I feel like this issue must effect everyone who makes cross-origin requests? 我觉得这个问题必须影响所有提出跨域请求的人?
OPTIONS请求是CORS的飞行前请求
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.