【发布时间】:2019-12-21 09:36:37
【问题描述】:
Angular HttpHeaders responseType: '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...
【问题讨论】:
-
尝试在 httpOptions 中的
responseType: 'text',之后添加observe: 'response',然后执行 console.log 响应。 -
您使用的是哪个版本的 Angular?
-
版本 7.2.0...
-
至于
observe: response,它。在 i 之前出现错误。可以做一个console.log -
post = this.httpClient.post(destinationUrl, httpBody, httpOptions); post.subscribe( (response) => { console.log(response); return response; }, (err) => { throw err;
标签: angular http-headers angular-httpclient