繁体   English   中英

在 Angular http Post 请求中获得完整响应

[英]Getting a full response in Angular http Post request

我正在尝试从 POST 请求中获得完整响应。 我已经阅读了如何获得 angular 官方网站上提到的获取请求的完整响应。 角 http

它说的是添加{ observe: 'response' } 但它适用于get请求而不适用于post请求。 post请求接受 2-3 个参数,所以我不能将它作为第四个参数发送。 请看看我的代码,让我知道我做错了什么。

    const httpOptions = {
        headers: new HttpHeaders({
          'Content-Type':  'application/json'
        })
    };

    return this.http.post('http://localhost:3000/api/Users/login', data, httpOptions, { observe: 'response' })
        .do( function(resp) {
            self.setSession(resp);
        });

这给了我一个错误,因为 4 个参数是不允许的。

编辑

接受的答案现在似乎不起作用。 我得到以下

error:
error TS2345: Argument of type '{ headers: HttpHeaders; observe: string; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...'.
  Types of property 'observe' are incompatible.
    Type 'string' is not assignable to type '"body"'.

所以:我需要更明确地声明 httpOptions 的类型:

const httpOptions: { headers; observe; } = {
  headers: new HttpHeaders({
    'Content-Type':  'application/json'
  }),
  observe: 'response'
};


return this.http.post('http://localhost:3000/api/Users/login', data, httpOptions) ...

observe应该是httpOptions一部分作为属性。

const httpOptions = {
    headers: new HttpHeaders({
      'Content-Type':  'application/json'
    }),
    observe: 'response'
};

 return this.http.post('http://localhost:3000/api/Users/login', data, httpOptions)
   .do( function(resp) {
        self.setSession(resp);
 });

设置选项如下:

const httpOptions = {
    headers: new HttpHeaders({
      'Content-Type':  'application/json'
    }),
    observe: 'response' as 'response'
};

将“响应”转换为“响应”将强制选择适当的过载。

暂无
暂无

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

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