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

Noo*_*ber 4 angular

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

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

    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);
        });
Run Code Online (Sandbox Code Playgroud)

这给我一个错误,因为不允许使用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"'.
Run Code Online (Sandbox Code Playgroud)

Wit*_*rba 6

所以:我需要更明确地声明 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) ...
Run Code Online (Sandbox Code Playgroud)


Sac*_*aka 5

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);
 });
Run Code Online (Sandbox Code Playgroud)