[英]http.put request is executed twice in Angular2
我正在嘗試更新從Angular2應用程序調用API的公司記錄。 我在調試時注意到http調用正在執行兩次。 我找到了另一個與此相同的stackoverflow線程 ,答案是添加.share()
因為熱和冷的Observables。 我已將此添加到我的http調用中,但這並沒有解決問題。 我感謝任何幫助!
company.service.ts
update(company: Company): Observable<Company> {
return this._http.put(URL_COMPANY, JSON.stringify(company), { headers: this.headers })
.map((res: Response) => company).share();
}
getCompanies() {
return this._http.get(URL_COMPANY)
.map((response: Response) => response.json()).share()
.catch(this.handleError);
}
getCompany(id: number): Promise<Company> {
const url = `${URL_COMPANY}/${id}`;
return this._http.get(url)
.toPromise()
.then(response => response.json() as Company)
.catch(this.handleError);
}
company.component.ts
ngOnInit(): void {
this.route.params.switchMap((params: Params) => this.companyService.getCompany(+params['id']))
.subscribe(company => this.company = company);
}
save(): void {
this.companyService.update(this.company).subscribe(
(worked) => { console.log("success")},
(error) => { console.log("failed")}
);
}
第一個呼叫是用於CORS的預檢請求 。
默認情況下, Same-origin策略禁止跨域請求,因此第一個請求是檢查跨域請求的容差。
如果單擊第一個請求,您將看到“請求方法:OPTIONS”,以及Angular HTTP模塊發出的此預檢請求,您無法對其執行任何操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.