0

I am trying to fetch data from database through Jquery DataTable and it gives error as:

"DataTables warning: table id=tblBindData - Cannot reinitialise DataTable."

function BindData() {

    $("#tblBindData").DataTable({
        "processing": true,
        "serverSide": true,
        "sAjaxSource": "FirstTask.aspx/PopulateDatatable",
        "fnServerData": function (sSource, aoData, fnCallback) {
            aoData.push({});
            $.ajax({
                "dataType": 'json',
                "contentType": "application/json; charset=utf-8",
                "type": "POST",
                "url": sSource,
                "data": aoData,
                "success": function (msg) {
                    var json = jQuery.parseJSON(msg.d);
                    fnCallback(json);
                    $("#tblBindData").show();
                },
                error: function (xhr, textStatus, error) {
                    if (typeof console == "object") {
                        console.log(xhr.status + "," + xhr.responseText + "," + textStatus + "," + error);
                    }
                }
            });
        }
    });

}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Kamran
  • 371
  • 1
  • 4
  • 15
  • are you getting the right response from server? – Anoop Joshi P Jun 19 '15 at 07:13
  • In the current version of DataTables (1.10.4) you can simply add destroy:true to the configuration to make sure any table already present is removed before being re-initialised. Refer this http://stackoverflow.com/a/26968754/795683 – Sain Pradeep Jun 19 '15 at 07:14

1 Answers1

1

There are some mistakes in your code,

  1. You have given dataType : json , then there is no need for using jQuery.parseJSON(msg.d).
  2. If you are using "contentType": "application/json; charset=utf-8", , then you should serialize your data before passing it to ajax. Better to remove contentType from yur ajax call.
Anoop Joshi P
  • 25,373
  • 8
  • 32
  • 53