Fahad
Fahad

Reputation: 173

Displaying JSON

I am returning JSON from my controller to AJAX success response. The JSON look likes:

{
   "success": false,
   "errors": {
      "ConfirmPassword": [
         "'Confirm Password' and 'Password' do not match.",
         "Password does not meet the criteria.",
         "The Confirm Password must be at least 6 characters long."
      ],
      "Password": [
         "Password does not meet the criteria.",
         "The Password must be at least 6 characters long."
      ]
   }
}

Now I wan to display data in div tag with formatting. I have tried :

$('#info').append(JSON.stringify(data));

but this is just a dump of whole string, which doesn't look nice in my view. Any idea how to do it. Ajax call :

 $.ajax({
            type: 'POST',
            url: '/Dashboard/UpdatePassword/',
            data: $("#password-update-form").serialize(),
            success: function (data) {
                if (data.success) {

                    //alert("test");
                }
                else {



                    $('#info').append(JSON.stringify(data));

                }

Upvotes: 0

Views: 34

Answers (1)

Fahad
Fahad

Reputation: 173

Thanks for all replies. I managed to sorted it.

  success: function (data) {
                if (data.success) {

                    alert("test");
                }
                else {


                    console.log(JSON.stringify(data.errors))

                    for (var prop in data.errors) {
                        //alert(prop + " is " + data.errors[prop]);                      
                         $('#val-form').append('<p>'+data.errors[prop]+'<p>');

                    }

Upvotes: 0

Related Questions