-1

I have this json object, and i want to get the value of the error but it always returning me an undefined result.

Here is the json object

{"isValid":false,"errors":["Username is required.","Password is required."]}

my code is:

success: function (response) {

                var JSONstring = JSON.stringify(response);                    
                var JSONobject = JSON.parse(JSONstring);

                alert(JSONstring);
                alert(JSONobject);



                console.log(JSONobject);                    
                var _result = JSONobject.errors;

i have also tried:

var _result = JSONobject[0]['errors'];
var _result = JSONobject['errors'][0];

but still i can't access the value.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • `JSON.stringify(response)` you dont do this step. It is already a string, unless it's parsed before hand which you wouldnt need either step in that case – Patrick Evans Mar 04 '18 at 04:38
  • Do you have `dataType: 'json'` in your `$.ajax` options? – Barmar Mar 04 '18 at 04:40
  • @Barmar yes i have. Here it is: $.ajax({ url: '@Url.Action("Login", "Home")', type: "POST", data: { UserName: $('#txtUsername').val(), Password: $('#txtPassword').val() }, dataType: "json", – Hudas Iskaryote Mar 04 '18 at 04:43
  • When you use `dataType: 'json'`, jQuery parses it for you. You don't need to call `JSON.stringify()` or `JSON.parse()` yourself. `response` contains the object. – Barmar Mar 04 '18 at 04:56
  • It returning me an undefined result if i don't call `JSON.stringify()` or `JSON.parse()` – Hudas Iskaryote Mar 04 '18 at 05:10

1 Answers1

0

If you already have access to the JSON object you can work with it without manipulation. If your question was also looking to get it in that form, see this StackOverflow answer for how to format your request.

success: function (response) {
    // Assuming response = {"isValid":false,"errors":["Username is required.","Password is required."]}

    // Directly access the property errors
    var errors = response.errors;
    console.log(errors); // ["Username is required.","Password is required."]

JSFiddle

William Fleming
  • 466
  • 2
  • 7
  • it returning me undefined when i try the above code. – Hudas Iskaryote Mar 04 '18 at 04:52
  • it gives me this `undefined` `"{\"isValid\":false,\"errors\":[\"Username is required.\",\"Password is required.\"]}"` `{"isValid":false,"errors":["Username is required.","Password is required."]}` `success: function (response) { var _result = response.errors; console.log(_result);` `var JSONstring = JSON.stringify(response); var JSONobject = JSON.parse(JSONstring);` – Hudas Iskaryote Mar 04 '18 at 05:01
  • `console.log(JSONstring); console.log(JSONobject);` – Hudas Iskaryote Mar 04 '18 at 05:03
  • If you provide us a bigger code sample on something like JSFiddle we could potentially help more. – William Fleming Mar 04 '18 at 05:12
  • Actually the response comes from my controller: `var _errors = new { isValid = ModelState.IsValid, errors = ModelState.SelectMany(x => x.Value.Errors) .Select(x => x.ErrorMessage) }; string errors = JsonConvert.SerializeObject(_errors); return Json(errors, JsonRequestBehavior.AllowGet);` – Hudas Iskaryote Mar 04 '18 at 05:19