Ionic 4-尝试将标题添加到不接受选项的post-post调用

问题描述 投票:0回答:2

我正尝试在帖子中发送标题,但似乎无法获得有效的选项值。

  sendPostRequest() {
    let token = this.storage.get('ACCESS_TOKEN');
    var headers = new Headers();
    headers.append('Accept', 'application/json');
    headers.append('Content-Type', 'application/json');
    headers.append('Authorization', 'Bearer ' + token);
    headers.append('responseType', 'text');
    let postData = this.signatureForm.value;
    this.httpClient.post("http://localhost:3000/signature", postData, { headers: headers })
      .subscribe(data => {
        this.presentToast();
      }, error => {
    });
  }

我在{ headers: headers }的编辑器中遇到错误错误消息是:

No overload matches this call.
  The last overload gave the following error.
    Type 'Headers' is not assignable to type 'HttpHeaders | { [header: string]: string | string[]; }'.
      Type 'Headers' is not assignable to type '{ [header: string]: string | string[]; }'.
        Index signature is missing in type 'Headers'.ts(2769)
http.d.ts(2431, 9): The expected type comes from property 'headers' which is declared here on type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }'
http.d.ts(2430, 5): The last overload is declared here.
angular ionic4
2个回答
0
投票

尝试这样:

const headers = new HttpHeaders().set('Content-Type', 'application/json');

this.httpClient.post("http://localhost:3000/signature", postData, { headers: headers })
  .subscribe(data => {
    this.presentToast();
  }, error => {
});

0
投票

它可以在httpOptions内部。您可以修改标题,例如

const httpOptions = {
      headers: new HttpHeaders({
         'Accept': 'application/json '
        'Content-Type': 'application/json', (add all the headers)
      });
    }



this.httpClient.post("http://localhost:3000/signature", postData, httpOptions)
      .subscribe(data => {
        this.presentToast();
      }, error => {
    });
热门问题
推荐问题
最新问题