4

My JSON data is:

[
    {
        "serviceName":"test",
        "requestXML":"<soapenvelope><uname>testuser</uname></soapenvelope>"
    },
    {
        "serviceName":"test2",
        "requestXML":"<soapenvelope><uname>testuser2</uname></soapenvelope>"
    } 
]

jQuery code to call to backend which returns this JSON data

var postRequest = $.post(url);

postRequest.done(function( data ) {

      $('#tableid').dataTable( {
         "processing": true,
          destroy: true,
          "aaData": data,
          "aoColumns": [
                { "data": "serviceName" },
                { "data": "requestXML" },
          ]
      });     
});

Now when it is shown on the screen as jQuery DataTable, I wanted the entire XML to be printed as it. But it just prints testuser instead of the whole XML.

Could anyone please help me with this as what is going wrong?

I have verified my JSON data is going correctly.

Gyrocode.com
  • 57,606
  • 14
  • 150
  • 185
V Joe
  • 271
  • 1
  • 5
  • 23

3 Answers3

4

SOLUTION

You can use $('<div/>').text(data).html() trick to encode HTML entities.

For example:

$('#tableid').dataTable({
    "processing": true,
    "destroy": true,
    "data": data,
    "columns": [
       { 
          "data": "serviceName" 
       },
       { 
          "data": "requestXML",
          "render": function(data, type, full, meta){
              return $('<div/>').text(data).html();
          }
       }
    ]
});

DEMO

See this jsFiddle for code and demonstration.

Gyrocode.com
  • 57,606
  • 14
  • 150
  • 185
0

Your server should take care of the response what it is sending. instead of sending

[
{"serviceName":"test","requestXML":"<soapenvelope><uname>testuser</uname></soapenvelope>"},
 {"serviceName":"test2","requestXML":"<soapenvelope><uname>testuser2</uname></soapenvelope>"} 
]

it should send something like below

[
    {"serviceName":"test","requestXML":"&lt;soapenvelope&gt;&lt;uname&gt;testuser&lt;/uname&gt;&lt;/soapenvelope&gt;"},
     {"serviceName":"test2","requestXML":"&lt;soapenvelope&gt;&lt;uname&gt;testuser2&lt;/uname&gt;&lt;/soapenvelope&gt;"} 
    ]

If that is not possible ( you don't control the server ) then the following link should help Escape markup in JSON-driven jQuery datatable?

Community
  • 1
  • 1
Abhiram mishra
  • 1,597
  • 2
  • 14
  • 34
0

First of all you will need XML escaping function (taken from here):

var XML_CHAR_MAP = {
  '<': '&lt;',
  '>': '&gt;',
  '&': '&amp;',
  '"': '&quot;',
  "'": '&apos;'
}

function escapeXml (s) {
  return s.replace(/[<>&"']/g, function (ch) {
    return XML_CHAR_MAP[ch]
  })
}

Then you can render cell content using it:

{
  data: "requestXML",
  render: function(data, method, object, pos) {
    return '<pre>' + escapeXml(data) + '</pre>'
  }
}

Now your XML should be visible. Remove pre tags, if not required.

dimakura
  • 7,575
  • 17
  • 36