简体   繁体   English

Angular 带有响应访问响应的发布请求 response.body 问题

[英]Angular Post request with response access response.body issue

I'm sending a POST request to my server and I need to access the response.body.我正在向我的服务器发送一个 POST 请求,我需要访问 response.body。

Unfortunately, I'm not finding a method of doing that不幸的是,我没有找到这样做的方法

The POST request POST 请求

bday(id: number) {
let params = this.userParams;
return this.http
  .post<User[]>(environment.apiUrl + 'user/bday/' + id, {
    observe: 'response',
    params,
  })
  .pipe(
    map((response) => {
// I need to access the response.body
        })
      );
  }

But I'm getting the error: Property 'body' does not exist on type 'User[]' no matter what I'm trying.但我收到错误消息:无论我尝试什么,“用户 []”类型都不存在属性“主体”。

I tried to declare the value response as HttpResponse<User[]> but that didn't work either.我试图将值响应声明为 HttpResponse<User[]> 但这也不起作用。

I'm wondering why I'm not able to access the body of the response?我想知道为什么我无法访问响应的正文?

I did a Get request and everything went smooth:我做了一个获取请求,一切都很顺利:

Get request获取请求

getUseres(params) {
return this.http
  .get<User[]>(environment.apiUrl + 'users', {
    observe: 'response',
    params,
  })
  .pipe(
    map((response) => {
      this.paginatedResult.result = response.body;
      if (response.headers.get('Pagination') != null) {
        this.paginatedResult.pagination = JSON.parse(
          response.headers.get('Pagination')
        );
      }
      return this.paginatedResult;
    })
  );

} }

Does anyone had that issue before and know how to fix it?以前有没有人遇到过这个问题并且知道如何解决它? Thanks!!!谢谢!!!

The post method signature is post(url, body, options) post 方法签名是post(url, body, options)

So its probably not working because your setting your options as the second argument instead of the third one.所以它可能不起作用,因为您将选项设置为第二个参数而不是第三个参数。

cheers干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM