我正在尝试显示消息电子邮件“ xxx@gmail.com”已被接收 从这个JSON
{"readyState":4,"responseText":"{\"Message\":\"The request is invalid.\",\"ModelState\":{\"\":[\"Name xxx is already taken.\",\"Email 'xxx@gmail.com' is already taken.\"]}}","responseJSON":{"Message":"The request is invalid.","ModelState":{"":["Name xxx is already taken.","Email 'xxx@gmail.com' is already taken."]}},"status":400,"statusText":"Bad Request"}
我已经检查了上一个问题link。我尝试了相同和不同的种类,如下所示。但是我失败了
fail(function (showError) {
console.log('Signup failed');
console.log('full error = ' + JSON.stringify(showError));
var response = JSON.stringify(showError);
console.log("response error = "+JSON.stringify(response.responseText.ModelState));
console.log('Message error = ' + JSON.parse(showError.responseText).Message);
console.log('ModelState error = ' + JSON.parse(showError.responseText).ModelState[0]);
//console.log('ModelState error val= ' + JSON.parse(showError.responseText).ModelState[0].val);
console.log('ModelState error text= ' + JSON.parse(showError.responseText).ModelState[0].text);
});
答案 0 :(得分:1)
ModelState
是一个以空字符串作为第一个键(包含错误数组)的对象-因此您可以执行以下操作:
let errors = JSON.parse(showError.responseText).ModelState[""];