Special characters in data from json file

Special characters in data from json file

ponasromasponasromas Posts: 2Questions: 2Answers: 0

Hello,

so here is part of code which retrieves json data.

$(document).ready(function () {

                var table = $('#records').DataTable({
                    dom: 'Bflrtip',
                    colReorder: true,
                    fixedHeader: true,
                    "ajax": {
                        url: '/data',
                        dataSrc: ''
                    },
                    "columns": [
                        {
                            "className": 'dt-control',
                            "orderable": false,
                            "data": null,
                            "defaultContent": ''
                        },
                        { "data": "date" },
                        { "data": "mta" },
                        { "data": "country" },
                        { "data": "env_from" },
                        { "data": "from" },
                        { "data": "subject" }
                    ],
                    "order": [[1, 'desc']],
                    select: {
                        style: 'os',
                        selector: 'td:not(:first-child)',
                        style: 'multi'
                    }
                });

JSON data example which is fed to the script above:

[{
country: "US"
date: "2022/05/10-17:33:23"
env_from: "blabermouth@domain.com"
from: "Blaber Mouth <b.mouth@domain.com>"
mta: "localhost"
subject: "Fw: Note from Blaber Mouth"
}]

Notice 'from:' field. When rendering table, it just displays 'Blaber Mouth', it cuts off <b.mouth@domain.com> part. How to escape/replace or encode special characters in data returned?

Answers

  • allanallan Posts: 63,538Questions: 1Answers: 10,476 Site admin

    The key thing to be aware of here is that DataTables doesn't attempt to escape any HTML data by default - so it is injecting that straight into the document and the browser is hiding it as a tag!

    Use the text rendering helper to stop that from happening.

    Allan

Sign In or Register to comment.