【问题标题】:DataTables: Uncaught TypeError: Cannot read property 'length' of undefined?数据表:未捕获的类型错误:无法读取未定义的属性“长度”?
【发布时间】:2014-07-24 03:33:49
【问题描述】:

所以,这是我对 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()只将数据填入业务和经济表对象中。

【问题讨论】:

  • 给我清楚。如果您想填写为什么不能只使用 GET 。我猜你没有正常返回。你能在你的控制器中发布返回码吗? mvc 使用权。
  • GET 方法的好主意。忘了这一切。谢谢。

标签: c# ajax json jquery-datatables


【解决方案1】:

我想这可能会影响您的数据表之旅。

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:当我对数据表不熟悉时,我从这些很棒的文章开始前进。这将为您提供更好的想法和示例项目:

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

问候

【讨论】:

  • 酷就是这样。任何问题随时在这个论坛上分享你的疑问:)
  • 好的,很抱歉再次打扰您,但是,您知道如何在没有 MVC 的情况下解决这个问题吗?我没有使用 MVC。很抱歉我没有早点说这个。我只是认为这无关紧要。
  • 没问题。我猜你正在使用 asp.net,如果你只需要在方法中返回一个 json 作为返回类型,那么 ajax 调用将与此类似,没有问题。有关更多信息,请查看:stackoverflow.com/questions/18244696/…。欢呼
猜你喜欢
  • 2012-12-13
  • 1970-01-01
  • 2013-11-24
  • 1970-01-01
相关资源
最近更新 更多