我正在尝试迭代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; }
}
的ScriptManager
<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]
答案 0 :(得分:0)
此处result.d
没有任何意义,它应该只是result
。
所以在你的情况下替换这个
$.each(result.d, function (key, val) {
// existing
}
使用
$.each(result, function (key, val) {
// existing
}
这将解决您的问题。