错误ajax错误-数据表警告:表id-示例

问题描述 投票:0回答:3

我单击“确定”并在控制台中出现错误:

enter image description here

enter image description here

我是编程新手,我需要帮助。我需要使用

json
从多个数据结构形成数据表。此时,我陷入了这个错误。请帮忙理解

控制器中的函数是json。

[HttpGet]
            public JsonResult Lowx()
            {
                var query = db.Infos.
                    Include(x => x.Profile).
                    Include(x => x.Cars).
                    ToList();

                return Json(new { data = query });
            }

表格和ajax

 <table class= "table" id="example" >
            <thead>
                <tr >
                    <th>first name</th>
                    <th>last name</th>
                    <th>middle name</th>
                    <th>birthday</th>
                    <th>carname</th>
                    <th>carnumber</th>
                </tr>
            </thead>
            <tbody></tbody>

        </table>

        <script src="~/Scripts/jquery-1.10.2.min.js"></script>
        <script src="~/Scripts/bootstrap.min.js"></script>
        <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
        <script src="~/Scripts/DataTables/jquery.dataTables.min.js"></script>
        <script type="text/javascript">

            $(document).ready(function (data) {
             $("#example").DataTable({
      ajax: {
          url: '@Url.Action("Lowx")',
          type: 'GET',

          dataSrc: ""

      },
      columns: [
        { data: "FirstName", name: "FirstName" },
        { data: "LastName", name: "LastName" },
        { data: "MiddleName", name: "MiddleName" },
        { data: "BirthDate", name: "BirthDate" },
        { data: "CarName", name: "CarName" },
        { data: "CarNumber", name: "CarNumber" }
      ]
    });

控制台:无法加载资源:服务器响应状态为 500(内部服务器错误)。

阿尔弗雷德和所有人的屏幕截图) enter image description here enter image description here

截图复制粘贴 enter image description here

c# jquery ajax asp.net-mvc datatables
3个回答
1
投票

尝试将此示例复制粘贴到您的视图文件中。当它工作正常时,更改 url 来解析您自己的数据,它应该可以工作。请注意,该操作是 POST,而不是 GET。

[HttpPost]
public JsonResult Lowx()
{
    var query = db.Infos.Include(x => x.Profile).Include(x => x.Cars).ToList();
    return Json(new { data = query });
}

http://jsfiddle.net/bababalcksheep/ntcwust8/

$(document).ready(function () {
    var url = 'http://www.json-generator.com/api/json/get/cbEfqLwFaq?indent=2';
    var table = $('#example').DataTable({
        'processing': true,
        'serverSide': true,
        'ajax': {
        'type': 'POST',
        'url': url,
        'data': function (d) {
            return JSON.stringify( d );
        }
     }
  });
  $('#reload').click(function (e) {
      table.ajax.reload();
  });
  $('.toggleCols').click(function (e) {
      e.preventDefault();
      var column = table.column( $(this).attr('data-column') );
      column.visible( ! column.visible() );
  });    
});

0
投票

请按如下方式声明DataTable:

$('#example').DataTable({
    "ajax": {
        "url": '@Url.Action("Lowx")',
        "dataSrc": ""
    },
    "columns": [
        { "FirstName", "data.Profile.FirstName" },
        { "LastName", "data.Profile.LastName" },
        { "MiddleName", "data.Profile.MiddleName" },
        { "BirthDate", "data.Profile.BirthDate" },
        { "CarName", "data.Cars.CarName" },
        { "CarNumber", "data.Cars.CarNumber" }
    ]
});

在 Chrome 中,查看

Network
选项卡以查看 Ajax 调用是否正确形成。在 Visual Studio 中,在
Lowx()
的开头放置一个断点以查看是否到达代码。请分享您的发现。


0
投票

Pls support this error comes
很久以来有没有任何解决方案,如果回复的话,不胜感激

© www.soinside.com 2019 - 2024. All rights reserved.