2012-10-14 4 views
0

JSON 객체를 반복하려고하는데 반복하지 못했습니다.jsonery ajax를 사용하여 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'); 
} 

EDIT

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

Val.Date는 대문자이지만 변수는 소문자로 선언됩니다. console.log (val)을 작성하면 각 루프 내부에 뭔가 출력됩니까? –

+0

console.log (결과)가 실행되면 어떻게됩니까? ? 당신이 돌아 오는 것을 우리에게 말해 줄 수 있습니까? –

+0

내가 console.log 결과를 얻을 때, 나는 객체 -> d -> 및 배열의 ​​4 항목을 얻고있다. – user829174

답변

0

result.d은 여기에 의미가 없으며 result이어야합니다.

그래서 귀하의 경우에는 문제가 해결됩니다

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

이이

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

를 교체합니다.

+0

console.log에 무엇이 들어 있는지에 관한 위의 편집을 참조하십시오. – user829174

관련 문제