0

JSON オブジェクトを反復しようとしていますが、反復できません。

cs:

[WebMethod]
public static List<TicVersion> GetTicVersionList()
{
    List<TicVersion> versionList = new List<TicVersion>
                                           {
                                               new TicVersion
                                                   {
                                                       Date = DateTime.Now,
                                                       Version = "2"
                                                   },
                                               new TicVersion
                                                   {
                                                       Date = DateTime.Now,
                                                       Version = "4"
                                                   },
                                               new TicVersion
                                                   {
                                                       Date = DateTime.Now,
                                                       Version = "13"
                                                   },
                                               new TicVersion
                                                   {
                                                       Date = DateTime.Now,
                                                       Version = "28"
                                                   },
                                           };
    return versionList;
}


public class TicVersion
{
    public DateTime Date { get; set; }
    public string Version { get; set; }
}

スクリプトマネージャー

<asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" EnablePartialRendering="true"
    runat="server" />

js:

$.ajax({
    type: "POST",
    url: "ManagerTic.aspx/GetTicVersionList",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    async: false, // TODO!
    success: function (result) {
        createTicVersionList(result);
    }
});


function createTicVersionList(result) {
    var items = [];
    $.each(result.d, function (key, val) { // here is the problem.... i am not being able to get the values
        items.push('<li id="' + val.Version + '" class="ui-widget-content">' + val.Date + '</li>'); // i need the actual Date and Version 
    });
    $('<ol/>', {
        'id': 'selectable',
        'class': 'ui-selectable',
        html: items.join('')
    }).appendTo('.baseVersion');
}

編集

> if i console.log(result) inside the each, i am getting 
> - Object
> - - d
> - - - [0]
> - - - [1]
> - - - [2]
> - - - [3]
4

1 に答える 1

0

はここresult.dでは意味がありませんresult

したがって、あなたの場合、これを置き換えます

$.each(result.d, function (key, val) {
    // existing 
}

$.each(result, function (key, val) {
    // existing 
}

これで問題が解決します。

于 2012-10-14T08:08:47.937 に答える