I have this dynamic json object
{
"servers":[
{
"comp1":{
"server1":{
"status":"boxup",
"ar":{
"0":"95.61"
},
"ip":{
"0":"192.168.1.0"
}
},
"server2":{
"status":"boxup",
"ar":{
"0":"99.5"
},
"ip":{
"0":"192.168.0.1"
}
}
}
},
{
"comp2":{
"server1":{
"status":"boxup",
"ar":{
"0":"88.39"
},
"ip":{
"0":"198.168.1.1"
}
},
"server2":{
"status":"boxup",
"ar":{
"0":"99.88"
},
"ip":{
"0":"192.168.0.1"
}
}
}
},
{
"comp3":{
"server1":{
"status":"none",
"ar":"none",
"ip":"none"
},
"server2":{
"status":"boxup",
"ar":{
"0":"99.97"
},
"ip":{
"0":"http:\/\/122.01.125.107"
}
}
}
},
{
"comp4":{
"server1":{
"status":"boxup",
"ar":{
"0":"95.64"
},
"ip":{
"0":"192.168.1.0"
}
},
"server2":{
"status":"boxup",
"ar":{
"0":"95.65"
},
"ip":{
"0":"192.168.1.2"
}
}
}
},
{
"comp5":{
"server1":{
"status":"boxup",
"ar":{
"0":"71.92"
},
"ip":{
"0":"192.168.1.0"
}
},
"server2":{
"status":"boxup",
"ar":{
"0":"98.89"
},
"ip":{
"0":"192.168.0.3"
}
}
}
}
]
}
and I tried to parse it with $.each (refer below)
$.ajax({
url:"/server-monitoring/load-servers",
type:'post',
dataType:'json',
success:function(e){
if(e.success){
$.each(e.servers,function(index,value){
//log the status from server1 on every comp
console.log(value.server1.status);
});
}
}
});
but unfortunately and sadly, it returns me an error (refer below)
Uncaught TypeError: Cannot read property 'status' of undefined
any help, ideas, suggestions, recommendation, clues please?
e.success? are you trying to check if the ajax call was a success?? or the message from the server has a success in it?comp[num]which is an object and within that is the server object which contains the status property.dataTypehas been set correctly. You don't need to parse it again.