Ionic 4 - 尝试将 headers 添加到 post - post 调用不接受选项
Ionic 4 - Trying to adding headers to post - post call not accepting options
我正在尝试用我的 post 发送 headers,但我似乎无法获得选项的有效值。
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 => {
});
}
I'm getting an error in my editor on ```{ headers: headers }```
Error message is:
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.
这样试试:
const headers = new HttpHeaders().set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.set('responseType', 'text')
.set('Authorization', 'Bearer ' + token);
this.httpClient.post("http://localhost:3000/signature", postData, { headers: headers })
.subscribe(data => {
this.presentToast();
}, error => {
});
可以在httpOptions
里面。你可以修改你的headers,比如
const httpOptions = {
headers: new HttpHeaders({
'Accept': 'application/json '
'Content-Type': 'application/json',
'responseType': 'text',
'Authorization': 'Bearer ' + token
});
}
this.httpClient.post("http://localhost:3000/signature", postData, httpOptions)
.subscribe(data => {
this.presentToast();
}, error => {
});
您正在使用 Headers
接口,您需要使用 @angular/common/http'
HttpHeaders
示例:
import { HttpHeaders } from '@angular/common/http';
var headers = new HttpHeaders();
headers.append('Accept', 'application/json');
//append more stuff
我正在尝试用我的 post 发送 headers,但我似乎无法获得选项的有效值。
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 => {
});
}
I'm getting an error in my editor on ```{ headers: headers }```
Error message is:
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.
这样试试:
const headers = new HttpHeaders().set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.set('responseType', 'text')
.set('Authorization', 'Bearer ' + token);
this.httpClient.post("http://localhost:3000/signature", postData, { headers: headers })
.subscribe(data => {
this.presentToast();
}, error => {
});
可以在httpOptions
里面。你可以修改你的headers,比如
const httpOptions = {
headers: new HttpHeaders({
'Accept': 'application/json '
'Content-Type': 'application/json',
'responseType': 'text',
'Authorization': 'Bearer ' + token
});
}
this.httpClient.post("http://localhost:3000/signature", postData, httpOptions)
.subscribe(data => {
this.presentToast();
}, error => {
});
您正在使用 Headers
接口,您需要使用 @angular/common/http'
HttpHeaders
示例:
import { HttpHeaders } from '@angular/common/http';
var headers = new HttpHeaders();
headers.append('Accept', 'application/json');
//append more stuff