[web] jquery ajax json data
ajax 기본 형태.
baseURL = 'http://[ip:port]/';
$.ajax({
type : 'get',
url : baseURL + 'device/info/',
dataType: 'json',
headers:{
Authorization: 'Token asdwae23f',
},
data : {username:'admin',password:'admin'},
success : function(data) {
// .... 서버와 통신 성공 시, 데이터 가공. 아래 참조
},
error : function(request, status, error) {
alert("code:"+request.status+"\n"+"message:"+request.responseText+"\n"+"error:"+error);
},
});
json 이 다차원 이라, value엔 json object가 올 경우.
만약 1차원이 온다면 value 는 123, 'aaef' 등 일정 값으로 전달 될 것이다.
success : function(data) {
var variable;
$.each(data, function(key, value) {
if(key == 'infos'){
$.each(value, function(key, value) { //MDS | DS
variable = key;
$.each(value, function(key, value) { //mds-1 | ds-2 | .....
$('#example').dataTable().fnAddData( [ "",variable, value.basic.hostname, value.basic.uptime, value.basic.health, value.basic.power, value.basic.ping ]);
}
}
}
}
}
json 값을 얻어 오기위해선 아래와 같이 '.'이나 '[]'를 이용 할 수 있다.
console.log(data.infos.MDS);
console.log(data.infos.MDS['anycloud-mds1'].ipmi);