从Ajax结果中获取特定值

时间:2017-09-25 05:05:37

标签: c# jquery ajax

我的代码运行正常。使用Ajax访问数据。从那个结果我想在标签中显示一个特定的值。当我使用下面的代码时,值正确显示

var i = parseInt(doc.d.length);
$("#<%=lblPresnet.ClientID %>").text(doc.d[5].PresentDays);

但我需要将我的代码更改为

var i = parseInt(doc.d.length);
$("#<%=lblPresnet.ClientID %>").text(doc.d[i].PresentDays);

在这里我发现了一个错误

Uncaught TypeError: Cannot read property 'PresentDays' of undefined

我不知道为什么会如此 我的Ajax代码

success: function (doc) {
console.log(doc);
var my_events = [];
if (!doc.result) {
$.each(doc.d, function (index, elem) {
    my_events.push({
        id: elem.EventName,
        title: elem.EventName,
        start: elem.StartDate,
        image: elem.ImageType,
    });
    var i = parseInt(doc.d.length);
    $("#<%=lblPresnet.ClientID %>").text(doc.d[5].PresentDays);
}); callback(my_events);
}
}

控制台结果

{d: Array(6)}d: Array(6)0: {EventID: "00000000-0000-0000-0000-000000000000", EventName: "Present", StartDate: "2017-08-01", PresentDays: 1, AbsentDays: 0, …}1: {EventID: "00000000-0000-0000-0000-000000000000", EventName: "Present", StartDate: "2017-08-02", PresentDays: 2, AbsentDays: 0, …}2: {EventID: "00000000-0000-0000-0000-000000000000", EventName: "Present", StartDate: "2017-08-03", PresentDays: 3, AbsentDays: 0, …}3: {EventID: "00000000-0000-0000-0000-000000000000", EventName: "Present", StartDate: "2017-08-04", PresentDays: 4, AbsentDays: 0, …}4: {EventID: "00000000-0000-0000-0000-000000000000", EventName: "Present", StartDate: "2017-08-07", PresentDays: 5, AbsentDays: 0, …}5: {EventID: "00000000-0000-0000-0000-000000000000", EventName: "Present", StartDate: "2017-08-08", PresentDays: 6, AbsentDays: 0, …}length: 6__proto__: Array(0)__proto__: Object

他们的任何解决方案

1 个答案:

答案 0 :(得分:0)

doc.d的长度为6,但其索引从0到5 ......我猜你应该将这些行改为

var i = parseInt(doc.d.length - 1);
$("#<%=lblPresnet.ClientID %>").text(doc.d[i].PresentDays);