如何订阅angular中的纯文本/文本响应



我的文章API返回纯lorem ipsum文本,但有效负载为JSON格式。我的问题是,我不能在订阅者的主块中获得响应,但能够在错误块中查看响应。请帮我修一下。

这是我的代码片段。

const path = 'sample URL';
const body = { 
id: 'xyz',
name: 'abc'
};
const httpOptions = {
headers: new HttpHeaders(
{
'Accept': 'text/plain',
'Content-Type': 'application/json',
'responseType': 'text'
})};
this.http.post(path, body, httpOptions).subscribe(
(res)=>{ },
err=>{ 
console.log("===",err);
}
);

我也尝试过res.text((,但没有成功。

您可以使用以下代码设置请求的响应类型:

this.http.post(URL, body, {responseType: 'text'})

您的代码中存在错误。响应类型不是标头的一部分。这是另一个类似header的选项。请参阅下面适用于您的代码。

const path = 'sample URL';
const body = {
id: 'xyz',
name: 'abc'
};
let headers = new HttpHeaders({
'Accept': 'text/plain',
'Content-Type': 'application/json',
})
this.http.post(path, body, {headers, responseType: 'text'}).subscribe((res) => {

最新更新