DataTables:Uncaught TypeError:无法读取属性' length'未定义?

时间:2014-06-03 17:20:39

标签: c# ajax json jquery-datatables

所以,这是我对DataTables表对象的AJAX调用。我试图让JSON填充只有行的表,因为它已经有标题行。

我不清楚DataTables ajax调用的语法是什么,因为它们从一个版本到另一个版本不同。

$('#MainContentPlaceHolder_business_return_flights').dataTable({
    "ajax": {
        "url": "Browse.aspx/GetBusinessFlights",
        "type": "POST",
        "contentType": "application/json; charset=utf-8",
        "dataType": "json"
    }
});

我一直收到错误:Uncaught TypeError: Cannot read property 'length' of undefined

这是返回的JSON:

{
  "d":{
  "draw":"1",
  "recordsTotal":"70",
  "recordsFiltered":"70",
  "aData":[
             [
                "BI 098",
                "London (LHR)",
                "Dubai",
                "08-08-2014",
                "12:55 PM",
                "11:55 PM",
                "Royal Brunei",
                "1",
                "0",
                "1300",
                "\u003cbutton type=\"button\" href=\"javascript:void(0)\" onclick=\"selectFlight($(this))\" data-toggle=\"oflight\" class=\"btn btn-block btn-info\"\u003eBook\u003c/button\u003e"
             ],
             [
                "CY 383",
                "Dubai",
                "Larnaca",
                "08-06-2014",
                "1:45 PM",
                "4:05 PM",
                "Cyprus Airways",
                "1",
                "0",
                "1100",
                "\u003cbutton type=\"button\" href=\"javascript:void(0)\" onclick=\"selectFlight($(this))\" data-toggle=\"oflight\" class=\"btn btn-block btn-info\"\u003eBook\u003c/button\u003e"
             ]
        ]
    }
}

更新:这是我的退货方式:

[WebMethod]
public static Dictionary<string, object> GetBusinessFlights()
{
    listRows = new List<List<string>>();
    business = new Table();
    economy = new Table();

    FillTable(economy, business, scheduledFlights.List);

    foreach (TableRow row in business.Rows)
    {
        listRow = new List<string>();

        foreach (TableCell cell in row.Cells)
        {
            listRow.Add(cell.Text);
        }

        listRows.Add(listRow);
    }

    field = new Dictionary<string, object>() { { "draw", "1" }, { "recordsTotal", economy.Rows.Count.ToString() }, { "recordsFiltered", economy.Rows.Count.ToString() }, { "aData", listRows } };

    return field;

}

注意: FillTable()仅将数据填入业务和经济表对象。

1 个答案:

答案 0 :(得分:3)

嗯,我想这可能会侵入你的数据表之旅。

DataTable填充数据:

$('#myDataTable').dataTable({
            "bProcessing": true,
            "bServerSide": true,
            "sAjaxSource": 'DataProvider', // This will be controller action method with Json return type which in turn fills your DataTable 
            "bJQueryUI": true,
            "aoColumns": [
                         { "sName": "ID"  },
                         { "sName": "COMPANY_NAME" },
                         { "sName": "ADDRESS" },
                         { "sName": "TOWN" }
            ]

        });

使用Ajax调用意味着您必须设置如下内容:

$.ajax({
                        "type": "GET",
                        "dataType": 'json',
                        "contentType": "application/json; charset=utf-8",
                        "url": //source url,
                        "data": {},
                        "success": function (data) {
                            //on success you will reach into it
                        }
                    });

你的控制器返回类型,如果这样的设置意味着它很酷:

在DataProvider操作方法

return Json(new
            {
                sEcho = param.sEcho, //communication b/w subsequent calls
                iTotalRecords = //your count here,
                iTotalDisplayRecords = //per page display records count,
                aaData = your array list which will bind to dataTable
            },
                        JsonRequestBehavior.AllowGet);

PS:当我不熟悉数据表时,我开始使用这些精彩的文章继续前进。这会给你更好的想法&amp;样本项目包括:

http://www.codeproject.com/Articles/155422/jQuery-DataTables-and-ASP-NET-MVC-Integration-Part

问候