1

我有以下内容:

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

    // Can I use my json post data?

    }, 'json');

我的帖子回复我有

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

我不知道如何让这个帖子回复显示在我的页面视图上。

我正在使用 MVC 4,并且我已经在此页面中有一个模型,下拉选择更改会触发此帖子。

请指教。

4

2 回答 2

2

您可以通过属性名称访问 JSON 数据:

$.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');
于 2013-03-01T18:57:15.490 回答
2

检查这个http://docs.jquery.com/Ajax/jQuery.post

这应该像这样简单:

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

如果您需要遍历 json 结果,请使用$.each

于 2013-03-01T18:58:20.087 回答