我有像这样的json响应,
{"readyState":4,"responseText":"{\"error\":{\"type\":\"Duplicate user\",\"errorMessage\":\"Another user exists with this user name\"}}","status":409,"statusText":"Conflict"}
我需要显示错误消息用户data.responseText.errorMessage
但是它显示错误,我刚刚使用JSON.stringify(data).replace(/\\/g, "")
方法删除了'\'(反斜杠)。但是我得到了一个字符串。(见下文)
{"readyState":4,"responseText":"{"error":{"type":"Duplicate user","errorMessage":"Another user exists with this user name (9846184100)"}}","status":409,"statusText":"Conflict"}
我无法使用JSON.parse
将此解析为json对象,但结果为Unexpected token e
然后我该如何显示errorMessage?
提前致谢
答案 0 :(得分:1)
您的字符串完全有效,但您必须首先解析响应文本:
var response = JSON.parse(data.responseText);
alert(response.error.errorMessage);