Skip to content Skip to sidebar Skip to footer

Datatables Build Table From Ajax Issue, Ok From File, Parameter Missing? No Data Available In Table

I have an issue with Datatables populating data from javascript. If I load the same javascript result from file it works perfectly using the 'ajax' attribute in Datatable parameter

Solution 1:

Your DataTables script is out of date. The old script used a different syntax for loading datatables with data from JavaScript.

Please refer to this JSfiddle to see your example working - you can use the following version of the DataTables script:

https://cdn.datatables.net/1.10.0/js/jquery.dataTables.js

Solution 2:

I have finally a working function as per my initial requirements.

As my jSon returned object contained column name information, [{ "meter": "test", "id": 15, "desc": "testDesc"}], I wanted to use this format as is instead of having to strip down the content as in case order of returned object is altered, the table works just the same.

Haven't got it working in jsFiddle yet. http://jsfiddle.net/j5a390d9/5/ but works ok in my application. Reason for jsFiddle is that I am doing an ajax call to a web service not exposed on internet. Have tried get response from an uploaded file on my website but cannot due to cross site scripting. Tried a proxy but still.

Anyways, this is my working code.

var dataset ;

        functionmeterData() {
            $.ajax({
                type: "POST",
                url: "jSonServices.asmx/getAllMeters",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: false,
                cache: false,
                timeout: 10000,
                success: function (msg) {
                    if ((msg).length < 5)
                        alert("No data Found!");
                    else {
                        //dataset =  [{ "meter": "test", "id": 15, "desc": "testDesc"}];// !important! Parse msg.d string into object                  var obj = JSON.parse(msg.d);                                       
                 $('#MeterDataTable').DataTable(
            {
                "aaData": obj,
                "aoColumns": [
                            { "mData": "meter" },
                            { "mData": "id" },
                            { "mData": "desc" }
                           ]
            }
            );
                    }
                },
                error: function (e) {
                    alert("not ok" + e.responseText);
                }
            });    

        }              

        $(document).ready(function () {
            meterData();            
        });

Post a Comment for "Datatables Build Table From Ajax Issue, Ok From File, Parameter Missing? No Data Available In Table"