【发布时间】:2015-08-24 13:50:20
【问题描述】:
我对返回数据的脚本进行了 ajax 调用。我在萤火虫中看到了数据,它是正确的。当数据回来时,我会进行排序,它也可以工作。我在萤火虫中看到对象按我想要的方式排序。排序后,我尝试访问键/值对。我在萤火虫中看到我想要的键/值在对象中。当我尝试使用对象键/值分配给 var 时,它是未定义的。我正在尝试汇总重复数据。如果我没有使用最好的方法,请帮助我使用更好的方法。
数据样本
coreDate "060115"
coreType "asterisk_11"
fileName "/var/www/html/Cores/gdb.hamid.asterisk.060115"
jid "hamid"
代码
$.when (
$.ajax( {
type:"GET",
url: 'cf.php',
dataType: 'json',
timeout: 120000,
})
)
.done(function(coreInfo) {
coreInfo.sort(function(a,b) { return a.coreType > b.coreType } );
var coreStats={};
coreStats.numOfCores=0;
coreStats.numOfCoreType=0;
coreStats.prevCoreType=coreInfo.coreType;
// for some reason coreInfo.coreType is not defined. The coreInfo object exists and coreType is there. I tried this.coreType but is undefined.
$.each(coreInfo, function() {
coreStats.numOfCores++;
if (coreStats.prevCoreType != this.coreType) {
// This works. why do I have to access this.coreType instead of coreInfo.coreType?
$('#list > tbody:last-child').append('<tr><td><a href="'+this.fileName+'">'+this.coreType+'</td><td>'+coreStats.numOfCoreType+'<br>core</td><td>jid</td></tr>');
coreStats.numOfCoreType=0;
}
coreStats.numOfCoreType++;
coreStats.prevCoreType=this.coreType;
// setting prevCoreType works here
});
$('#cores').html("<h1>"+coreStats.numOfCores+"</h1><br>Core Files");
})
.fail(function() {
});
【问题讨论】:
标签: javascript jquery ajax object