本文介绍了在Angular http Post请求中获得完整响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从POST请求获得完整的响应.我已经阅读了如何获得对angular官方站点上提到的get请求的完整响应. Angular http

I am trying to get full response from a POST request. I have read how to get full response for a get request mentioned at the official site of angular.Angular http

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

What it says is to add { observe: 'response' }. But it would work for a get request and not for post request. post request accepts 2-3 arguments, so I cannot send this as the 4th argument. Please have a look at my code and let me know what I am doing wrong.

    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个参数.

This gives me an error as 4 arguments are not allowed.

修改

目前已接受的答案似乎无效.我得到以下

The accepted answer seems to be not working now. I am getting the following

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 的一部分.

observe should be part of the httpOptions as a property.

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);
 });

这篇关于在Angular http Post请求中获得完整响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 21:50