【问题标题】:how to call http post in custom scenario like this in angular?如何在这样的自定义场景中以角度调用http post?
【发布时间】:2021-08-31 17:40:36
【问题描述】:

我已经创建了自定义发布方法,其签名与原来的几乎相同,

postcall(url: string, body: any | null, options: {headers?: HttpHeaders | {
          [header: string]: string | string[];
      };
       params?: HttpParams | {
          [param: string]: string | string[];
      };
      reportProgress?: boolean;
      responseType?: 'arraybuffer' | 'blob' | 'json' | 'text';
      withCredentials?: boolean;
  } = {}): Observable<any> {

    return this.http.post<any>(url,body,options.Headers?Headers:responseType);


  }

我无法通过上述 post call 传递选项作为参数。请告诉我如何传递参数。它应该传递我们在 postcall() 的正式参数中收到的选项。

编辑-1,

我的电话是

this.http.postcall(this.resumeapiserver,body,{responseType: 'text'});

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

this.http.postcall(this.apiServer + '/applicants/', JSON.stringify(applicant), this.httpOptions)

这两个都应该工作..

【问题讨论】:

  • 向我们展示更多代码.. 请向我们展示您希望通过邮寄方式发送的选项
  • @manzapanza 我的电话会是这样的,this.http.postcall(this.apiServer + '/applicants/', JSON.stringify(test), this.httpOptions) 或 this.http.postcall (this.apiserver,body,{responseType: 'text'});这两个电话都应该工作
  • 为了帮助我们帮助您,您应该使用所有相关代码编辑您的问题.. 我建议您删除您放在那里的代码,并放置没有的真实代码作品
  • @manzapanza 已编辑,请再检查一次。

标签: angular http-post


【解决方案1】:

试试这样的:

  post(applicant: any) {
    const options = {
      headers: new HttpHeaders({
        'Content-Type': 'application/json',
      }),
    };
    this.http.post(this.apiServer + '/applicants/', applicant, options);
  }

您的postcall 函数有错误,应该将这一行更改如下:

    return this.http.post<any>(url, body, options);

在我看来,postcall 函数没有多大意义,因为它是 httpClient post 函数的副本。所以,也许最好直接使用我在第一个示例代码中向您展示的 post 函数。

【讨论】:

  • 实际上我想创建自己的 http 服务类来处理所有 post call。这也不起作用 this.http.post(url, body, options.responseType);我想使用任何选项,不仅是 options.responseType
  • 得到一个错误“没有重载匹配这个调用。”在我的问题的最后一个截图。
  • 你的options参数声明类型错误..
猜你喜欢
  • 2015-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多