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

2024-04-28

我正在尝试从 POST 请求获得完整响应。我已经阅读了如何获得角度官方网站上提到的获取请求的完整响应。角度 http https://angular.io/guide/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 个参数。

Edit

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

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) ...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Angular http Post 请求中获取完整响应 的相关文章

随机推荐