Как читать мои сложные данные JSON Stringify в JQuery? - PullRequest
0 голосов
/ 13 марта 2019

Я пытаюсь отобразить сообщение Электронная почта '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"}

Я уже проверил свой предыдущий вопрос ссылка.Я пробовал такой же и другой вид, как показано ниже.Но мне не удалось

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);
    });

1 Ответ

1 голос
/ 13 марта 2019

ModelState - это объект с пустой строкой в ​​качестве первого ключа (который содержит ваш массив ошибок) - так что вы можете сделать:

let errors = JSON.parse(showError.responseText).ModelState[""];
...