数据表Ajax调用

时间:2015-06-19 07:09:40

标签: c# jquery asp.net ajax

我试图通过Jquery DataTable从数据库中获取数据,它会产生错误:

  

“DataTables警告:table id = tblBindData - 无法重新初始化   数据表“。

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);
                    }
                }
            });
        }
    });

}

1 个答案:

答案 0 :(得分:1)

您的代码中存在一些错误,

  1. 您已dataTypejson,则无需使用jQuery.parseJSON(msg.d)
  2. 如果您使用的是"contentType": "application/json; charset=utf-8",,那么您应该在将数据传递给ajax之前对其进行序列化。最好从你的ajax电话中删除contentType