Persistent Error: Requested unknown parameter '0' for row '0' column '0'

Persistent Error: Requested unknown parameter '0' for row '0' column '0'

malxxxmalxxx Posts: 15Questions: 2Answers: 0

I posted this question also on StackOverflow. I have tried my hardest to make use of datatables for the project.. but it just won't work. I've read all guides concerning flat Json formats
and I've troubleshoot it myself. Now, I can only humbly ask for help as to what is causing this error.. since I've encountered it BEFORE and I only had to add "dataSrc='' " to fix it..

What seems to be the problem? what is the error? how can I avoid this in the future?

This question has an accepted answers - jump to answer

Answers

  • malxxxmalxxx Posts: 15Questions: 2Answers: 0

    Additional info: in truth there are more than that 1 column for JSON, html and the Jquery I displayed. I reduced it to that to see if it was formatting error.

  • allanallan Posts: 63,691Questions: 1Answers: 10,500 Site admin

    Your SO question contains more information that this post, and from the code and information there, it appears that it should work.

    Could you run the debugger or link to the page please? That will hopefully give me enough information to understand why it isn't working.

    Allan

  • malxxxmalxxx Posts: 15Questions: 2Answers: 0

    my sincere thanks allan for taking notice of this problem

    some additional info:
    -the page is structured in a way that makes use of a masterpage where all js and css are loaded.
    -The site where the JSOn is being loaded is a .aspx file which gets it's JSON string from Newtonsoft JSON

    I will also try this debugger and see why it does not output.. please keep me company a while longer sir Allan

  • malxxxmalxxx Posts: 15Questions: 2Answers: 0

    I ran the debugger in google chrome by running the page, copying and pasting the code on the console part of inspect, it ran and then gave me this

    Thank you very much for the help..

  • malxxxmalxxx Posts: 15Questions: 2Answers: 0

    here is another debugger results. I fixed my error with the setInterval (I wanted my datatables to refresh every 10 seconds in case there are new records) The datatable refreshes properly now, but still, the error persists

  • kthorngrenkthorngren Posts: 21,453Questions: 26Answers: 4,975

    The JSON data returned is not correct. This is the first 3 records returned:

    [{
        "UserName": "ABENS"
    }, {
        "UserName": "AILPAL"
    }, {
        "UserName": "ANDREW.GUILLERMO"
    }, {......
    
    {
        "UserName": "VIRGINIAP"
    }]
    

    You have defined your columns to use objects:

        "columns": [{
            "data": "UserName"
        }],
    

    The return JSON needs to be in a data object and should look more like this:

    {"data":
    [{
        "UserName": "ABENS"
    }, {
        "UserName": "AILPAL"
    }, {
        "UserName": "ANDREW.GUILLERMO"
    }, {......
    
    {
        "UserName": "VIRGINIAP"
    }]
    
    }
    

    You can see an example of this by looking at the AJAX tab of this page:
    https://datatables.net/examples/ajax/objects.html

    Kevin

  • malxxxmalxxx Posts: 15Questions: 2Answers: 0

    Thanks Kevin for the response, but I have to make use of the Flat format.. this is the JSON the .aspx url is leaving.. It was also mentioned in the docs here that the flat file format is accepted.
    that is also why I included dataSrc ="".
    is tehre a way to solve this?

  • malxxxmalxxx Posts: 15Questions: 2Answers: 0

    Reason why I cannot change the source JSON: this is what the team is using, and said JSON will always be produced in this format..

  • malxxxmalxxx Posts: 15Questions: 2Answers: 0

    Is there another approach I can use that uses the FLAT json format? like push and such? Please I just need a sample that works...

  • kthorngrenkthorngren Posts: 21,453Questions: 26Answers: 4,975
    Answer ✓

    I included dataSrc =""

    Yes, you are right about loading the flat file. I compared your debug to that of the flat data source example:
    https://datatables.net/examples/ajax/custom_data_flat.html

    One difference I see is the example has both responseText and responseJSON objects in the jqXHR object. You can see the debug output of the example here. This can be seen in the debugger output under Full table state. I tried your config and data (in a text file) on my system and it works and I also see the responseJSON object.

    Someone else can confirm but it seems the lack of having the responseJSON object might be causing the issue.

    Kevin

  • malxxxmalxxx Posts: 15Questions: 2Answers: 0

    thank you Kevin. So how do I add said responseJSON to my code? what do I need to do basically?

  • malxxxmalxxx Posts: 15Questions: 2Answers: 0
    edited March 2017

    IT WORKS NOW, DAMN!
    I switched columns: data for aoColumns: mData
    I also added mDataProp:"" alongside dataSrc: ""

    WHY? WHY IS IT LIKE THIS? WHY IS data DIFFERENT FROM mData?
    WHY aoColumns INSTEAD OF columns?

    ..still, phenomenal work with the plugin. Thank you very much @allan and @kthorngren

  • kthorngrenkthorngren Posts: 21,453Questions: 26Answers: 4,975

    I'm not familiar with aspx but you probably need to set the response content type, in the aspx script, to something like "application/json; charset=utf-8" to allow it to return a json header.

    I believe, by default, Datatables sets the dataType to json in the ajax request and will expect the response to contain the json header.

    Kevin

  • malxxxmalxxx Posts: 15Questions: 2Answers: 0

    New problem though, buttons will not appear.
    I did it like this :
    `var tbl = $('#tblUserAccountsManagement').DataTable({

                ajax: {
    
                    url: "AccountsManagementJSON.aspx",
                    dataSrc: "",
                    mDataProp: ""
                },
    
                aoColumns: [
                    {
                        //data: 'UserName',
                        mData: 'UserName'
                    },
                    { mData: 'UserRoleCode' },
                    { mData: 'IsActive' },
                    { mData: 'FullName' },
                    { mData: 'Email' },
                    { mData: 'Company' },
                    { mData: 'TIN' }, 
                    { mData: 'SSS' },
                    { mData: 'Address1' },
                    { mData: 'Address2' },
                    { mData: 'Address3' }
    
                ],
                autofill: true,
                //order: [[0, 'asc']],
                select: true,
                responsive: true,
                buttons: true,
                length: 10,
    
                /*exporting */
                dom: 'Bfrtip',
                buttons: [
                    'copy', 'csv', 'excel', 'pdf', 'print'
                ]
    
    
            });`
    

    What could be the problem? Here is the debugger
    Also yes @kthorngren I did set the response type to "application/json; charset=utf-8"

  • kthorngrenkthorngren Posts: 21,453Questions: 26Answers: 4,975

    Do none appear or does the Copy button appear?

    If only the Copy then you probably will need to load the additional JS files for the other buttons as shown here:
    https://datatables.net/download/release#Buttons

    There was a forum discussion earlier today about some buttons not being compatible with some versions of Safari. If using Safari try a different browser.

    Kevin

  • malxxxmalxxx Posts: 15Questions: 2Answers: 0

    @kthorngren thanks man, the problem with the buttons was that I switched to jQuery slim. I switched back to regular afterwards (3.1.1)

  • Andy@GAOAndy@GAO Posts: 16Questions: 3Answers: 0
    edited March 2017

    Hi there, I have the same problem that malxx had in the asp mvc razor universe
    but no solution.

        $('#example').DataTable({
            processing: true,
            serverSide: true,
            sServerMethod: 'POST',
            ajax: {
                url: '/Forms/GetMetricDLRList',
                dataSrc: '',
                mDataProp: '',
                type: 'POST'
            },
            //data: mydata.data,
            aoColumns: [
            { mData: "metricId" },
            { mData: "MetricName" },
            { mData: "dataSource" },
            { mData: "lastRunDate" }
            ],
            order: [[0, 'asc']]
        });
    });
    

    The error is:
    DataTables warning: table id=example - Requested unknown parameter 'metricId' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4

    I'm getting this back (just 1st column example for now)

    [{"metricId":"76"},
    {"metricId":"77"},
    {"metricId":"78"}
    ]

    instead of

    {"data":
    [{"metricId":"76"},
    {"metricId":"77"},
    {"metricId":"78"}
    ]
    }

    Is there a way of forcing the "data:" parameter to be included in the json data that I get back from the server? Am I doing something obviously wrong here.
    Just tearing my hair out why I couldn't get malxx's solution to work.

    -Andy

  • kthorngrenkthorngren Posts: 21,453Questions: 26Answers: 4,975

    Probably would be good to look at the full response from the server. There is more than just the data the should be in the response when using server side processing. If you can't post a link to your page then start with the debugger.

    Kevin

  • allanallan Posts: 63,691Questions: 1Answers: 10,500 Site admin

    Firstly I would ask if you actually need server-side processing? Are you working with 50k records or more? If so, fine, keep it - but if less then remove it. Server-side processing will just add complexity.

    The issue is that your JSON return from the server is not complete for server-side processing. You have to return an object rather than just a plain array for server-side processing, since it needs information other than just the data to display.

    Allan

  • Andy@GAOAndy@GAO Posts: 16Questions: 3Answers: 0
    edited March 2017

    Hi Allan, I've figured out how to get the data back from the server, only problem now is for sorting and that's depending on getting an http: request back from the server

    but nothing coming back, am I doing something obviously wrong here?

    STOP PRESS sorted, will post details of solution for edification of others tomorrow.

    Thanks, Andy

  • Andy@GAOAndy@GAO Posts: 16Questions: 3Answers: 0
    edited March 2017

    Hi There, firstly the function call I got to work was thus:

    $(document).ready(function () {
        //alert("in the function");
    
        $('#example').DataTable({
            "processing": true,
            "serverSide": true,
            "info": true,   // control table information display field
            "stateSave": true,  //restore table state on page reload,
            "lengthMenu": [[10, 20, 50, -1], [10, 20, 50, "All"]],    // use the first inner array as the page length values and the second inner array as the displayed options
            "ajax": {
                "url": "@string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~"))/Forms/GetMetricDLRList",
                "type": "GET"
            },
            "columns": [
            { "defaultContent": "<button type='button' class='btn btn-primary btn-xs'>Trial Run</button>" },
            { "data": "metricId", "orderable": true },
            { "data": "MetricName", "orderable" : true },
            { "data": "dataSource", "orderable" : true },
            { "data": "lastRunDate", "orderable" : true },
            { "data": "runDataLoad", "orderable" : true },
            { "data": "trialRun", "orderable": true },
            { "data": null, className: 'checks', defaultContent:"<input type='checkbox' class='i-checks' name ='input[]'>" }
        ]
        });
    
    });
    

    This is working off sql server tables.
    also to do with tables how do you get the row data for the row retrived?

    I tried this

        $('#example tbody').on('click', 'button', function () {
            var data = table.row($(this).parents('tr')).data();
            alert("Name = " + data[1]);
        });
    

    It works with simple td based table data but not from a database, any ideas?

    Thanks Andy

  • allanallan Posts: 63,691Questions: 1Answers: 10,500 Site admin

    data[1]

    You are accessing it like an array, but you've configured DataTabels to expect objects (and indeed are loading it with objects). Use data.metricId.

    Allan

This discussion has been closed.