传递JSON URL时,jQuery数据表导致“无法读取未定义的属性'mData'”错误

时间:2019-12-06 17:29:27

标签: jquery datatables

我遇到了错误

  

“无法读取未定义的属性'mData'”

当我尝试获取数据表以显示JSON时。我不确定到底是什么情况,我不确定这是错误的URL,错误的HTML格式,错误的JSON还是其他完全错误的内容。这是JSON:

[ 
   { 
      "cSoldBy":"Foy, Patrick",
      "dSoldDate":"\/Date(1575180000000)\/",
      "cSoldTo":"Nancy Myers",
      "cCar":"2019 Kia Soul Base",
      "nSalePrice":21000.00,
      "nMSRP":20000.00
   },
   { 
      "cSoldBy":"Santiago, Gabirel",
      "dSoldDate":"\/Date(1572584400000)\/",
      "cSoldTo":"Bram Stoker",
      "cCar":"2020 Kia Soul Base",
      "nSalePrice":20000.00,
      "nMSRP":21000.00
   },
   { 
      "cSoldBy":"Sareen, Jesminder",
      "dSoldDate":"\/Date(1569906000000)\/",
      "cSoldTo":"HP Lovecraft",
      "cCar":"2020 Kia Soul Base",
      "nSalePrice":21750.00,
      "nMSRP":23000.00
   }
]

这是HTML:

    <table id="SalesList" class="display compact hover stripe order-column" style="width:70%">
        <thread>
            <tr>
                <th>Salesperson</th>
                <th>Sale Date</th>
                <th>Purchaser</th>
                <th>Car Sold</th>
                <th>Sale Price</th>
                <th>MSRP</th>
            </tr>
        </thread>
    </table>

这是Jquery:

@section Scripts{
    <script src="http://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#SalesList').DataTable({
                "ajax": {
                    "url": "/SalesPerson/saleslist/101"
                }
            });
        });
    </script>
    }

下面有人问我有关JSON的URL。这是该代码:

        public JsonResult SalesList(int ID)
        {
            IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["AutoDealer"].ConnectionString);

            List<ViewModels.DashboardSalesViewModel> SalesList = new List<ViewModels.DashboardSalesViewModel>();
            SalesList = db.Query<ViewModels.DashboardSalesViewModel>("usp_Get_DashboardSales", new { nUserID = ID }, commandType: CommandType.StoredProcedure).AsList();


            return Json(SalesList, JsonRequestBehavior.AllowGet);
        }

1 个答案:

答案 0 :(得分:0)

jcruz的评论为我指明了正确的方向。有两个重叠的问题:

  1. 默认数据源是“数据”。源可以不命名,您只需要在jquery脚本中将数据源声明为。
  2. 该类中值的名称与html中的名称不同。我必须通过手动声明其名称将值的名称映射到列。这是更正后的jQuery脚本:
@section Scripts{
    <script src="http://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#SalesList').DataTable({
                ajax: {
                    url: '/SalesPerson/saleslist/101',
                    dataSrc:''
                },
                columns: [
                    { data: 'cSoldBy' },
                    { data: 'dSoldDate' },
                    { data: 'cSoldTo' },
                    { data: 'cCar' },
                    { data: 'nSalePrice' },
                    { data: 'nMSRP' }

                ]
            });
        });
    </script>
    }

感谢大家的帮助。