我不确定这是一个错误还是我遗漏了什么,但我正在开发最新版本的 angular。
我正在向 API 发送请求,API 返回一个空主体,但我想将响应 header 返回给组件。
下面的代码
add (productslist: number[]):Observable<HttpHeaderResponse> {
return this.http.put<Response>(this.PutUrl, productslist, this.putheader).pipe(
tap(_ => console.log("add")),
// Response(),
catchError(this.handleError<number[]>(`addShoppingCart `)),
map((res:HttpHeaderResponse) =>{
console.log(res);
return res} )
);
}
res
未定义是我在这里做错了什么还是最新版本中存在已知错误。
最佳答案
代码:
add (productslist: number[]):Observable<HttpHeaderResponse> {
return this.http.put<Response>(this.PutUrl, productslist, {observe: 'response', headers: this.putheader}).pipe(
tap(_ => console.log("add")),
// Response(),
catchError(this.handleError<number[]>(`addShoppingCart `)),
map((res:HttpHeaderResponse) =>{
console.log(res);
return res} )
);
}
observe: It defines whether we want complete response or body only or events only. We need to assign values for observe property such as response for complete response, body for response with body and events for response with events.
关于angular 5 http post header 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49807590/