首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在这样的自定义场景中调用angular中的http post?

如何在这样的自定义场景中调用angular中的http post?
EN

Stack Overflow用户
提问于 2021-08-31 17:40:37
回答 1查看 30关注 0票数 0

我已经创建了自定义的post方法,与原始签名几乎相同,如下所示,

代码语言:javascript
复制
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);


  }

我无法将option作为参数传递,上面的post call.Please让我知道如何传递参数。它应该像我们在postcall()的形参中接收到的那样传递选项。

编辑-1,

我的电话是

代码语言:javascript
复制
this.http.postcall(this.resumeapiserver,body,{responseType: 'text'});

代码语言:javascript
复制
 httpOptions = {
    headers: new HttpHeaders({
      'Content-Type': 'application/json',
      
    })
  }

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

这两种方法都应该有效..

EN

回答 1

Stack Overflow用户

发布于 2021-08-31 18:08:35

试试这样的东西:

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

您的postcall函数有一个错误,可能应该更改此行,如下所示:

代码语言:javascript
复制
    return this.http.post<any>(url, body, options);

在我看来,这个postcall函数并没有多大意义,因为它是httpClient post函数的副本。因此,也许更好的做法是直接使用post函数,就像我在第一个示例代码中展示的那样。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69003166

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档