如何在 angular 中订阅 plain/text 响应
How to subscribe plain/text response in angular
我的 post 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 的一部分。它是另一个选项,例如 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) => {
我的 post 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 的一部分。它是另一个选项,例如 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) => {