다음 예는 Ajax 호출에서 XML 응답을 받고 테이블에 추가하는 예입니다.Datatables에 대한 JSON 응답?
$("#ajaxform5").submit(function(e) {
var formURL = $(this).attr("action");
var request = $.ajax({
url : formURL,
type: "GET",
contentType : "application/xml",
headers: {
"Type" : "events"
},
success: function(xml){
$(xml).find('event').each(function(){
var $event = $(this);
var id = $event.find("id").text();
var level = $event.find("level").text();
var name = $event.find("name").text();
var attributes01 = $event.find("attributes").children("entry").eq(0).children("string").eq(0).text();
var attributes02 = $event.find("attributes").children("entry").eq(0).children("string").eq(1).text();
var attributes11 = $event.find("attributes").children("entry").eq(1).children("string").eq(0).text();
var attributes12 = $event.find("attributes").children("entry").eq(1).children("string").eq(1).text();
var attributes21 = $event.find("attributes").children("entry").eq(2).children("string").eq(0).text();
var attributes22 = $event.find("attributes").children("entry").eq(2).children("string").eq(1).text();
var attributes31 = $event.find("attributes").children("entry").eq(3).children("string").eq(0).text();
var attributes32 = $event.find("attributes").children("entry").eq(3).children("string").eq(1).text();
var attributes41 = $event.find("attributes").children("entry").eq(4).children("string").eq(0).text();
var attributes42 = $event.find("attributes").children("entry").eq(4).children("string").eq(1).text();
var userId = $event.find("userId").text();
var ipaddress = $event.find("ipaddress").text();
var outcome = $event.find("outcome").text();
var html = '<tr><td>' + id + '</td><td>' + level + '</td><td>' + name + '</td><td>' +
'<div><span class="title">' + attributes01 + '</span> <span>' + attributes02 + '</span></div>' +
'<div><span class="title">' + attributes11 + '</span> <span>' + attributes12 + '</span></div>' +
'<div><span class="title">' + attributes21 + '</span> <span>' + attributes22 + '</span></div>' +
'<div><span class="title">' + attributes31 + '</span> <span>' + attributes32 + '</span></div>' +
'<div><span class="title">' + attributes41 + '</span> <span>' + attributes42 + '</span></div>' +
'</td><td>' + userId + '</td><td>' + ipaddress + '</td><td>' + outcome + '</td></tr>';
$('#eventTable').append(html);
});
},
error: function(jqXHR, textStatus, errorThrown) {
alert(textStatus);
}
});
});
이제 Datatables을 사용하고 싶습니다. 그래서 xml2json (abdmob/x2js)을 사용하여 응답 XML을 JSON으로 변환하면 아래와 같은 JSON 응답이 나타납니다.
JSON 응답;
{
"list" : {
"event" : [{
"dateTime" : "1473858435162",
"nanoTime" : "438326047248251",
"id" : "15680",
"eventTime" : {
"time" : "1473851193487",
"timezone" : "Asia/Istanbul"
},
"level" : "INFORMATION",
"name" : "Get message content",
"attributes" : {
"entry" : [{
"string" : ["metaDataIds", "6"]
}, {
"string" : ["messageId", "2765"]
}, {
"string" : ["channel", "Channel[id=44ab0667-d42c-496c-bca2-2bd7eecd6dc9,name=API jQuery Direct]"]
}
],
"_class" : "linked-hash-map"
},
"outcome" : "SUCCESS",
"userId" : "1",
"ipAddress" : "127.0.0.1",
"serverId" : "f85e48e2-32b3-429f-bee5-31630d8143a2"
}, {
"dateTime" : "1473858435167",
"nanoTime" : "438326052034149",
"id" : "15679",
"eventTime" : {
"time" : "1473851191973",
"timezone" : "Asia/Istanbul"
},
"level" : "INFORMATION",
"name" : "Get messages by page limit",
"attributes" : {
"entry" : [{
"string" : ["filter", "MessageFilter[maxMessageId=2765,minMessageId=<null>,originalIdUpper=<null>,originalIdLower=<null>,importIdUpper=<null>,importIdLower=<null>,startDate=<null>,endDate=<null>,textSearch=<null>,textSearchRegex=<null>,statuses=<null>,includedMetaDataIds=[6],excludedMetaDataIds=<null>,serverId=<null>,contentSearch=[],metaDataSearch=<null>,textSearchMetaDataColumns=<null>,sendAttemptsLower=<null>,sendAttemptsUpper=<null>,attachment=false,error=false]"]
}, {
"string" : ["includeContent", "false"]
}, {
"string" : ["offset", "0"]
}, {
"string" : ["limit", "21"]
}, {
"string" : ["channel", "Channel[id=44ab0667-d42c-496c-bca2-2bd7eecd6dc9,name=API jQuery Direct]"]
}
],
"_class" : "linked-hash-map"
},
"outcome" : "SUCCESS",
"userId" : "1",
"ipAddress" : "127.0.0.1",
"serverId" : "f85e48e2-32b3-429f-bee5-31630d8143a2"
}
]
}
}
그래서 내 첫 번째 질문, DataTables 유효이 JSON 응답은?
유효하다면, 첫 번째 예제에서와 같이 객체 및 배열을 행/열로 어떻게 처리 할 수 있습니까?
Datatables에 JSON 응답을 삽입하려했지만 시도 할 수 없었습니다. 그림에서 결과를 볼 수 있습니다. response added to rows char by char
나는 아래의 코드를 삽화로 만 제공 했으므로 데이터를 삽입하기 위해 얼마나 침을 흘리지 않았는지 알기를 원하지 않습니다.
array.data 옵션을 사용하여 배열을 읽었지만 다시 실패했습니다.
$("#ajaxform6").submit(function(e) {
var formURL = $(this).attr("action");
var request = $.ajax({
url : formURL,
type: "GET",
contentType : "application/xml",
headers: {
"Type" : "events"
},
success: function(data, textStatus, jqXHR) {
var x2js = new X2JS();
var bXML = jqXHR.responseText;
var jsonObj = x2js.xml_str2json(bXML);
var json = JSON.stringify(jsonObj);
console.log(json);
$('#table_id').DataTable({
data: json,
bSort: false,
});
},
error: function(jqXHR, textStatus, errorThrown) {
alert(textStatus);
}
});
});
도움 주셔서 감사합니다. @abhijeet. 한 가지 더 두 가지 데이터를 한 필드에 어떻게 추가 할 수 있습니까? 예를 들어 한 필드에 ID 및 레벨 표시를 원합니다. – Erdogan
환영합니다 @ObsessiO – Abhijeet
내 의견을 업데이트했지만이 @Abhijeet에 대한 알림을받지 못했습니다. 한 필드에 어떻게 두 개의 데이터를 추가 할 수 있습니까? 예를 들어 한 필드에 "ID"와 "레벨"을 표시하고 싶습니다. – Erdogan