作者:StormyXin | 来源:互联网 | 2023-01-26 18:08
Imworkingonaprojectwhichhasrailsapiasaback-endandangularasafrontend.Inonepartic
I'm working on a project which has rails api as a back-end and angular as a front end. In one particular point I need to make a text/plain call. Even though I set the content-type to 'text/plain', HttpClient tries to parse payload to json. I can't figure out why it behaves like that.
我正在开发一个项目,它有一个rails api作为后端,有角度作为前端。在一个特定点上,我需要进行文本/普通呼叫。即使我将content-type设置为'text / plain',HttpClient也会尝试将有效负载解析为json。我无法弄清楚为什么它会像那样。
Rails back-end:
Rails后端:
def getTranslations
render plain: 'some plain text'
end
Angular Client:
角度客户:
headers = new HttpHeaders({
"Content-Type": "text/plain",
"Accept": "text/plain"
});
this.http.get('http://localhost:3000/getTranslations', { headers: this.headers })
.map((res:Response) => {
console.log(res);
return res.text()
})
.subscribe(
res => {
console.log(res);
},
err => {
console.log(err);
}
)
Response:
响应:
"Http failure during parsing for http://localhost:3000/getTranslations" Unexpected token s in JSON
“解析http:// localhost:3000 / getTranslations期间的Http失败”JSON中出现意外的令牌
Thanks.
谢谢。
1 个解决方案