Error Invalid JSON. But JSON is valid?

Error Invalid JSON. But JSON is valid?

whaliimwhaliim Posts: 5Questions: 3Answers: 0

Error messages shown:
Invalid JSON response
Description of problem:

I want to create datatables with in table controls like this. I have created the code but somehow still getting Invalid JSON response Error. I have checked the response and it is a valid JSON response. Could someone tell me what am I missing here?

Thank you in advance.

**HTML **

div class="">
            <table id="list" class="display">
                <thead>
                    <tr class="bold">
                        <th>Shop</th>
                        <th>art</th>
                        <th>date</th>
                        <th>time</th>
                        <th>Name</th>
                        <th></th>
                        <th></th>
                    </tr>
                </thead>
                <tfoot>
                    <tr class="bold">
                        <th>Shop</th>
                        <th>art</th>
                        <th>date</th>
                        <th>time</th>
                        <th>Name</th>
                        <th></th>
                        <th></th>
                    </tr>
                </tfoot>
            </table>
        </div>
} );

JS

$(document).ready( function () {
var columnsConfig = {
            default: [
                { "data": "company", "orderable": false },
                { "data": "type", "orderable": false },
                { "data": "date", "orderable": false },
                { "data": "time", "orderable": false },
                { "data": "fullname", "orderable": false },
                {
                    "data": null,
                    "className": "dt-center editor-view",
                    "defaultContent": '<a class="btn btn-primary editbutton"><i class= "fa fa-eye" ></i></a>',
                    "orderable": false
                },
                {
                    " "data": null,
                    "className": "dt-center action-view",
                    "defaultContent": '<a class="btn btn-primary editbutton"><i class= "fa fa-check" ></i></a>',
                    "orderable": false
                }
            ]
        };
window.dt_table = $('#list').DataTable({
                "scrollX": true,
                "searchDelay": 1000,
                "createdRow": function (row, data, dataindex) {
                    $(row).attr('data-id', data.id);
                },
                "language": {
                    "url": "\German.json"
                },
                "processing": true,
                "serverSide": true,
                "columns": columnsConfig.default,
                "ajax": {
                    "url": "Default.aspx/getData",
                    "dataSrc": "data",
                    "type": "POST",
                    "contentType": "application/json; charset=utf-8",
                    "dataType": "json",
                    "data": function (d) {
                        return JSON.stringify({ parameters: d });
                    },
                    "dataFilter": function (res) {
                        var parsed = JSON.parse(res);
                        return parsed.d;
                    }
                }
            });
} );

JSON Response: (parsed.d)

{
    "__type": "ProgramClass",
    "draw": 1,
    "recordsTotal": 1,
    "recordsFiltered": 1,
    "data": [{
            "id": "1",
            "company": "Company Name",
            "date": "01.03.2021",
            "time": "16:00",
            "type": "Service",
            "firstname": "Max",
            "lastname": "Mustermann",
            "phone": null,
            "fullname": "Max Mustermann"
        }
    ]
}

Answers

  • allanallan Posts: 63,461Questions: 1Answers: 10,466 Site admin

    Nothing obviously wrong in what you’ve got above - could you link to a test case showing the issue so I can trace it through please?

    Allan

  • whaliimwhaliim Posts: 5Questions: 3Answers: 0

    I'm afraid I cannot prepare a test case. Otherwise, I would have prepared it.

    However, apparently I have to change dataType: json on my ajax parameter to dataType: text to make it work. Strange, because I thought parsed.d is already a JSON object.

This discussion has been closed.