angular 5 http post header 响应

标签 angular http http-headers response angular5

我不确定这是一个错误还是我遗漏了什么,但我正在开发最新版本的 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/

相关文章:

ios - 如何在 iOS Swift 中使用 Alamofire 将 iPhone 视频上传到服务器?

java - 使用网络类获取 url 地址

ruby-on-rails - 如何覆盖 Rails 4 中 Controller 或操作的 X-Frame-Options

javascript - Plunker 在几天前有效的脚本上给我 404

angular - Azure Bot Framework DirectLine v3 开始对话 - 错误 403 禁止

angular - 汇总 commonjs

Angular 6 - 如何向 Material 表添加分页

json - 如何从 Go http.Request 中检索对象?

php - 如何在登录后重定向一个人

PHP cURL 如何添加用户代理值或克服服务器阻止 cURL 请求?