[英]Decode http response in Angular - JSON.parse issue
After quite a bit of messing around, I've got to the point where I can recieve the following body in a 404 error response from my backend. 经过一番混乱之后,我到达了可以从后端收到404错误响应中的以下正文的地步。 I'm struggling to parse the content into angular so I can use it.
我正在努力将内容解析为有角度的内容,以便可以使用它。 I'm know this is simple stuff, so sorry to ask such a basic question.
我知道这很简单,很抱歉提出这样一个基本问题。 The _body looks like this.
_body看起来像这样。
_body: "{\"httpStatus\":404,\"errorType\":\"NotFound\",\"message\":\"Device does not exist!\"}"
These are fine: 这些很好:
console.log("Err = ", err);
Err = Response {_body: "
{\"httpStatus\":404,\"errorType\":\"NotFound\",\"message\":\"Device does not exist!\"}", status: 404, ok: false, statusText: "OK", headers: Headers, …}
and: 和:
console.log("Err Body : ", err._body);
Err Body :
{\"httpStatus\":404,\"errorType\":\"NotFound\",\"message\":\"Device does not exist!\"}
But this doesn't work: 但这不起作用:
let errorObject = eval(errorString);
Uncaught (in promise): SyntaxError: Invalid or unexpected token
... ...
var errBody = JSON.parse(errorString);
console.log("JS err body", errBody);
Error: Uncaught (in promise): SyntaxError: Unexpected token \ in JSON at position 1
But I can't figure out how to get the individual fields out. 但是我不知道该如何处理各个字段。 I'm aware that the above efforts are naive and wrong.
我知道上述努力是幼稚和错误的。 I'm sure anyone with JS or angular skills can solve this in a minute.
我敢肯定,具有JS或棱角技巧的人都可以在一分钟内解决此问题。
PS cut me some slack. PS让我有些懈怠。 I'm a hardware designer.
我是硬件设计师。 I'm here because I don't know something, which is always the best reason to ask a question.
我在这里是因为我一无所知,这始终是问问题的最佳理由。
Thanks for the answers. 感谢您的回答。 JSON.parse does not work for me!?
JSON.parse对我不起作用!
SyntaxError: Unexpected token \ in JSON at position 1
I looked more closely at what you had success with, and I agree it works fine in the console. 我仔细研究了您的成功做法,并同意它可以在控制台中正常工作。 But it does not work for me in Angular.
但这在Angular中对我不起作用。 What did work was:
起作用的是:
let errBody = JSON.parse("\"" + err._body + "\"");
Although it seems ridiculous to do. 尽管这样做似乎很荒谬。 Especially since, afterwards, the result is not quite right:
尤其是之后,结果并不完全正确:
err body {"httpStatus":404,"errorType":"NotFound","message":"Device does not exist!"}
If I then try to get at errBody.message, it's undefined!... This is totally absurd. 如果我随后尝试获取errBody.message,则它是未定义的!...这完全荒谬。 What am I doing wrong?
我究竟做错了什么? How do you guys do this for a living?
你们如何以此为生? It's killing me!
这太痛苦了!
I'm assuming errorString
is err._body
? 我假设
errorString
是err._body
? Either way, parsing that string to JSON should be as simple as: 无论哪种方式,将该字符串解析为JSON都应该很简单:
let error = JSON.parse(err._body);
I came back to this recently. 我最近又回来了。 And finally managed to figure it out.
最终设法弄清楚了。 I needed to remove some unwanted backslashes in the body before trying JSON Parse.
在尝试JSON Parse之前,我需要删除体内的一些不需要的反斜杠。
const errorStringReplaced = err._body.replace(/\\/g, '');
const errBody = JSON.parse(errorStringReplaced);
this.outcomeMessage = errBody.message;
Having done that, I could then grab the innards properly. 完成此操作后,我就可以正确抓住内脏了。 I'd still prefer to send the object properly in the first place, but this will have to do for now.
我仍然希望首先正确地发送对象,但是现在必须这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.