invalid json it says!!

invalid json it says!!

maznabilimaznabili Posts: 2Questions: 1Answers: 0
edited March 2019 in Free community support

Dear all,
I've been getting this error and I don't know what to do..
It says..
"Uncaught Error: DataTables warning: table id=grid - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1"

Although json lint/formatter says my json is valid..
what to do now?

here is my json outcome..

{
"data": [{
"SMID": 1869576,
"FromNum": "611500000",
"ToNum": "611550000",
"MessageText": "some text, few words",
"SendDate": "\/Date(1546290000527)\/",
"Done": 0,
"UserID": 2754
}, {
"SMID": 1869577,
"FromNum": "66170497888",
"ToNum": "66171675858",
"MessageText": "Media: type: image/jpg - file name: img-20181230-wa0016.jpg",
"SendDate": "\/Date(1546290001670)\/",
"Done": 1,
"UserID": 5352
}, {
"SMID": 1869578,
"FromNum": "611508158855",
"ToNum": "611556659881",
"MessageText": "some text, few words",
"SendDate": "\/Date(1546290006817)\/",
"Done": 1,
"UserID": 2754
}, {
"SMID": 1869579,
"FromNum": "66170497888",
"ToNum": "6613798279",
"MessageText": "Media: type: image/jpg - file name: img-20181230-wa0016.jpg",
"SendDate": "\/Date(1546290008540)\/",
"Done": 1,
"UserID": 5352
}, {
"SMID": 1869580,
"FromNum": "611508158855",
"ToNum": "611565560601",
"MessageText": "some text, few words",
"SendDate": "\/Date(1546290014530)\/",
"Done": 0,
"UserID": 2754
}, {
"SMID": 1869581,
"FromNum": "66170497888",
"ToNum": "66176112593",
"MessageText": "Media: type: image/jpg - file name: img-20181230-wa0016.jpg",
"SendDate": "\/Date(1546290015593)\/",
"Done": 1,
"UserID": 5352
}, {
"SMID": 1869582,
"FromNum": "611508158855",
"ToNum": "611595583875",
"MessageText": "some text, few words",
"SendDate": "\/Date(1546290021540)\/",
"Done": 0,
"UserID": 2754
}, {
"SMID": 1869583,
"FromNum": "66170497888",
"ToNum": "66171027098",
"MessageText": "Failed media",
"SendDate": "\/Date(1546290023097)\/",
"Done": 0,
"UserID": 5352
}, {
"SMID": 1869584,
"FromNum": "611508158855",
"ToNum": "611544112626",
"MessageText": "some text, few words",
"SendDate": "\/Date(1546290028560)\/",
"Done": 0,
"UserID": 2754
}, {
"SMID": 1869585,
"FromNum": "66170497888",
"ToNum": "6613372159",
"MessageText": "Media: type: image/jpg - file name: img-20181230-wa0016.jpg",
"SendDate": "\/Date(1546290029770)\/",
"Done": 1,
"UserID": 5352
}],
"draw": null,
"recordsTotal": 2306848,
"recordsFiltered": 2306848
}

Answers

  • kthorngrenkthorngren Posts: 20,372Questions: 26Answers: 4,780
    edited March 2019

    Not sure if this is causing the error but this part is not correct:

    "draw": null,
    "recordsTotal": 2306848,
    "recordsFiltered": 2306848
    

    According to the server side processing docs the draw parameter is expected to be an integer. The draw parameter is a sequence and your server script is expected to return the sequence number sent to it.

    Kevin

  • maznabilimaznabili Posts: 2Questions: 1Answers: 0

    I double checked the response.. it is as below

    {
        "data": [{
            "SMID": 2033648,
            "FromNum": "611508158855",
            "ToNum": "611508158844",
            "MessageText": "some text",
            "SendDate": "\/Date(1546741837993)\/",
            "Done": 1,
            "UserID": 1601,
            "IsMedia": true
        }, {
            "SMID": 2033650,
            "FromNum": "963933990930",
            "ToNum": "966509994854",
            "MessageText": "some text",
            "SendDate": "\/Date(1546741839773)\/",
            "Done": 1,
            "UserID": 1601,
            "IsMedia": true
        }],
        "draw": 2,
        "recordsTotal": 33,
        "recordsFiltered": 33
    }
    

    And my Javascript is like this:

    $("#btnSubmit").click(function () {
        getData();
    })
    
        $.fn.dataTable.ext.errMode = 'throw';
    
    var getData = function () {
        $.ajax({
            type: "Get",
            url: "/SentMessages/json",
            contentType: "application/json; charset=utf-8",
            data: {
                failed: $('#failed').val(),
                success: $('#success').val(),
                blocked: $('#blocked').val(),
                from: $('#fromDate').val(),
                to: $('#toDate').val(),
                fromNumber: $('#phone').val(),
                toNumber: $('#toNum').val(),
                searchText: $('#msg').val()
            },
            success: function (data) {
                bindDataToTable(data);
            },
            error: function (xhr) {
                //alert(xhr);
                //alert(xhr.responseText);
                var n = document.createElement('script');
                n.setAttribute('language', 'JavaScript');
                n.setAttribute('src', 'https://debug.datatables.net/debug.js');
                document.body.appendChild(n);
            }
        })
    }
    
    var bindDataToTable = function (data) {
        $("#grid").DataTable({
            //serverSide: "true",
            serverSide: true,
            //processing: "true",
            processing: true,
            language: {
                "processing": "Fetching Data... please be patient"
            },
            order: [0, "asc"],
            //dataSrc: data,
            contentType: "json",
            Columns: [
                        { "data": "SMID", "name": "SMID" },
                        { "data": "FromNum", "name": "FromNum" },
                        { "data": "ToNum", "name": "ToNum" },
                        { "data": "MessageText", "name": "MessageText", "orderable": false, },
                        {
                            "data": "SendDate", "name": "SendDate", "render": function (value) {
                                if (value === null) return "";
                                const monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
                                var pattern = /Date\(([^)]+)\)/;
                                var results = pattern.exec(value);
                                var dt = new Date(parseFloat(results[1]));
                                return dt.getFullYear() + "/" + monthNames[(dt.getMonth() + 1)] + "/" + dt.getDate();
                            }
                        },
                        { "data": "Done", "name": "Done" },
                        { "data": "IsMedia", "name": "IsMedia" }
            ]
        })
    }
    
  • kthorngrenkthorngren Posts: 20,372Questions: 26Answers: 4,780

    I suspect the problem is due to defining serverSide: true but not using ajax within the Datatables init code. It also looks like you have some ajax config options (lines 46 and 47) in the Datatables init code which aren't doing anything. Please add use the ajax option instead of the external ajax request with server side processing.

    You can use ajax.data to pass the parameters to the server. The docs show some examples. When using the ajax option make sure you don't use the success function as noted in the docs.

    Kevin

  • colincolin Posts: 15,163Questions: 1Answers: 2,588

    Yep, as Kevin said, if you've enabled serverSide, then DataTables needs to be in control of the Ajax requests. If you don't need server-side processing, just remove that option and you'll be good.

    C

This discussion has been closed.