Odd ajax object parsing problem.

Odd ajax object parsing problem.

jeffmowensjeffmowens Posts: 3Questions: 2Answers: 0

Hello. We have an intranet application we're experimenting using DataTables with that is behaving oddly.

We have a very simple ajax call to a web service:

var tickets = $('#tblProjectTickets').DataTable({
                "processing": false,
                ajax: {
                    url: '/Services/ProjectTickets.svc/GetAll',
                    type: 'POST'
                }
            });

The web service returns JSLint verified formatted JSON data:

[{"GroupName":"Managed","Id":"7","Name":"Project B","OrderId":"123710"},
{"GroupName":"Managed","Id":"7","Name":"Project B","OrderId":"123710"},
{"GroupName":"Managed","Id":"7","Name":"Project B","OrderId":"123710"},
{"GroupName":"Managed","Id":"7","Name":"Project B","OrderId":"123710"},
{"GroupName":"Managed","Id":"7","Name":"Project B","OrderId":"123710"},
{"GroupName":"Managed","Id":"7","Name":"Project B","OrderId":"123710"},
{"GroupName":"Managed","Id":"7","Name":"Project B","OrderId":"123710"},
{"GroupName":"Managed","Id":"7","Name":"Project B","OrderId":"123710"}]

We have verified this is what is being return via F12 developer tools AND by viewing the raw data in an alert box by adding the following to the initialization:

...
dataSrc: function (json) {
                        alert(JSON.stringify(json));
                        return json;
                    }

The problem: When the page runs, the table populates with 8 rows (as per the data size) but all the columns show "null" in them. Further we get the infamous "unknown parameter '0' for row 0" error.

We are new to DataTables but have read through all the documentation and still cannot figure this out. All the references on the web refer to the previous 1.9x versions so hopefully someone here will have a solution on this latest DataTables release.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,636Questions: 1Answers: 10,092 Site admin
    Answer ✓

    Two things to do here:

    1. Use ajax.dataSrc and set it to be an empty string. You can use a function if you want, but no need - the empty string tells DataTables to just use the plain array.
    2. Use columns.data to tell DataTables what object name parameter you want for each column. You are getting an error about index 0 not existing since DataTables uses indexes by default (it can't guess the object parameter name!). This tech note (4) has more information about that error.

    Allan

  • jeffmowensjeffmowens Posts: 3Questions: 2Answers: 0

    Thanks Allan!

This discussion has been closed.