Angular HttpHeaders 响应类型:'text'。给出“类型 'string' 不可分配给类型 'json'”
Angular HttpHeaders responseType: 'text' . giving 'Type 'string' is not assignable to type 'json'"
Angular HttpHeaders 响应类型:'text'。给予 Type 'string' is not assignable to type 'json'
。错误。我知道响应不是 JSON。我不明白如何更改类型?我想获取该文本 (HTML) 并在之后用正则表达式对其进行解析。
代码
const httpOptions = {
headers: new HttpHeaders({
accept: '*/*',
contentType: 'application/x-www-form-urlencoded',
}),
responseType: 'text',
};
post = this.httpClient.post(destinationUrl, httpBody, httpOptions);
post.subscribe(
(res) => {
console.log(res)
},
(error) => {
console.error('download error:', error)
},
() => {
console.log('Completed')
},
);
控制台出错
如您所见,响应类型为文本。由于我不明白的事情,我无法让它接受文本,因为它正在等待 json...
根据official angular documentation
Overload #3 Constructs a POST request that interprets the body as a
text string and returns the response as a string value.
post(url: string, body: any, options: { headers?: HttpHeaders | {
[header: string]: string | string[]; }; observe?: "body"; params?:
HttpParams | { [param: string]: string | string[]; }; reportProgress?:
boolean; responseType: "text"; withCredentials?: boolean; }):
Observable Parameters url string The endpoint URL.
body any The content to replace with.
options object HTTP options
Returns Observable: An Observable of the response, with a
response body of type string.
通过查看文档,我们可以做这样的事情。响应(在主体内)是字符串:
clickMe() {
this.httpClient.post(
`https://reqres.in/api/users`,
{
name: "paul rudd",
movies: ["I Love You Man", "Role Models"],
},
{
observe: 'response',
responseType: 'text',
}
).subscribe(x => console.log('x: ', x.body))
}
我将 HTTPOptions 作为对象解决了这个问题
let HTTPOptions:Object = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
}),
responseType: 'text'
}
return this.http.post<any>(url, body, HTTPOptions ).pipe(
catchError(this.handleError)
);
Angular HttpHeaders 响应类型:'text'。给予 Type 'string' is not assignable to type 'json'
。错误。我知道响应不是 JSON。我不明白如何更改类型?我想获取该文本 (HTML) 并在之后用正则表达式对其进行解析。
代码
const httpOptions = {
headers: new HttpHeaders({
accept: '*/*',
contentType: 'application/x-www-form-urlencoded',
}),
responseType: 'text',
};
post = this.httpClient.post(destinationUrl, httpBody, httpOptions);
post.subscribe(
(res) => {
console.log(res)
},
(error) => {
console.error('download error:', error)
},
() => {
console.log('Completed')
},
);
控制台出错
如您所见,响应类型为文本。由于我不明白的事情,我无法让它接受文本,因为它正在等待 json...
根据official angular documentation
Overload #3 Constructs a POST request that interprets the body as a text string and returns the response as a string value.
post(url: string, body: any, options: { headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType: "text"; withCredentials?: boolean; }): Observable Parameters url string The endpoint URL.
body any The content to replace with.
options object HTTP options
Returns Observable: An Observable of the response, with a response body of type string.
通过查看文档,我们可以做这样的事情。响应(在主体内)是字符串:
clickMe() {
this.httpClient.post(
`https://reqres.in/api/users`,
{
name: "paul rudd",
movies: ["I Love You Man", "Role Models"],
},
{
observe: 'response',
responseType: 'text',
}
).subscribe(x => console.log('x: ', x.body))
}
我将 HTTPOptions 作为对象解决了这个问题
let HTTPOptions:Object = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
}),
responseType: 'text'
}
return this.http.post<any>(url, body, HTTPOptions ).pipe(
catchError(this.handleError)
);