0

I am trying to display the message Email 'xxx@gmail.com' is already taken from this 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"}

I already checked with my previous question link. I tried the same and different kind also like below. But I failed

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);
    });
Liam neesan
  • 2,282
  • 6
  • 33
  • 72
  • 1
    Not even sure if the `responseText` is valid, but chrome parses it. `ModelState` is an object with an empty string for the key - so `JSON.parse(showError.responseText).ModelState[""][0]` – tymeJV Mar 13 '19 at 19:21
  • @tymeJV yes, It's working. please make it in answer. I will accept it. – Liam neesan Mar 13 '19 at 19:24

1 Answers1

1

ModelState is an object with an empty string as the first key (which contains your array of errors) - so you can do:

let errors = JSON.parse(showError.responseText).ModelState[""];
tymeJV
  • 103,943
  • 14
  • 161
  • 157