1

I have the following:

    $.post('@Url.Action("getName")', postData, function(data) {

    // Can I use my json post data?

    }, 'json');

My post response I have

   {"Id":1,"Name":"John", "Age": 24}

I don't know how to get this post response to show on my page view.

I am using MVC 4 and I have a model in this page already that on dropdown select change triggers this post.

Please advice.

tereško
  • 58,060
  • 25
  • 98
  • 150
NoviceDeveloper
  • 1,270
  • 3
  • 15
  • 41

2 Answers2

2

You access the JSON data via property name:

$.post('@Url.Action("getName")', postData, function(data) {

    // Can I use my json post data?
    // Yes...
    // Given: data == {"Id":1,"Name":"John", "Age": 24}
    $('#someField').val(data.Name);
    $('#someOtherField').val(data.Age);
    // etc...

}, 'json');
M.Ob
  • 1,785
  • 14
  • 27
2

Check this http://docs.jquery.com/Ajax/jQuery.post

This should work as simple as this:

 $.post('@Url.Action("getName")', postData, function(data) {    
     alert(data.Id);
     console.log(data.Name);
     console.log(data.Age);
    }, 'json');

If you need to iterate through json results use $.each

Community
  • 1
  • 1
ssilas777
  • 9,672
  • 4
  • 45
  • 68