在ajax调用后,为第0行第0列请求未知参数“ 0”

时间:2019-10-11 08:50:39

标签: ajax datatables

我想在从服务器获取数据时使用服务器端渲染。当我单击数据表上的页面调度按钮时,我想从服务器上载新数据。因此,我为此使用了以下代码。但是我得到这个错误 “ DataTables警告:表id = MyDataTable-请求的未知参数'CompanyName'用于第0行第0列。有关此错误的更多信息,请始终参见http://datatables.net/tn/4”。

$text = "Established in 2004, AssistanZ has become one of the most amazing things on the planet.";
$oldvalue = strip_tags("Established in <b>2004</b>, <strong>AssistanZ</strong> has become one of the most");
$newValue = "I am one of the";

echo str_replace($oldvalue, $newValue, $text);

我的查看页面是这个

 public class HomeController : Controller
    {

        public async Task<ActionResult> Index()
        {

            return View();
        }   

        public async Task<JsonResult> GetEmployeeRecord(DataTablesParam param)
        {
            var a = await new CompanyService().Getlisttt(x => x.Phone1 == "12345688");
            List<CompanyViewObject> List = new List<CompanyViewObject>();
            int pageNo = 1;

            if (param.iDisplayStart >= param.iDisplayLength)
            {

                pageNo = (param.iDisplayStart / param.iDisplayLength) + 1;

            }

            int totalCount = 0;


            totalCount = a.Count();

            List = a.Skip((pageNo - 1) * param.iDisplayLength).Take(param.iDisplayLength).Select(x => new CompanyViewObject
            {
                CompanyName = x.CompanyName,
                Description = x.Description,
                GlobalCompanyID = x.GlobalCompanyID,
                Phone1 = x.Phone1,
                CompanyTypeName = x.CompanyTypeName,
                CompanyOwnershipName = x.CompanyOwnershipName,
                CompanyIndustryName = x.CompanyIndustryName,
                ParentID = x.ParentID,
                Website = x.Website,
                EMail = x.EMail,
                Phone2 = x.Phone2,
                Fax = x.Fax,
                Confirmed = x.Confirmed,
                Active = x.Active

            }).ToList();



            return Json(new
            {
                aaData = List,
                sEcho = param.sEcho,
                iTotalDisplayRecords = totalCount,
                iTotalRecords = totalCount
            });
        }
}

预先感谢

编辑=当我像这样更改代码时,我没有收到错误,但仍然无法获取表上的值

<div class="row pt-10">

    <div class="col-md-12">
        <table class="display" id="MyDataTable">
            <thead>
                <tr>
                    <th>
                        CompanyName
                    </th>
                    <th>
                        Description
                    </th>
                    <th>
                        GlobalCompanyID
                    </th>
                    <th>
                        Phone1
                    </th>
                </tr>
            </thead>

            <tbody></tbody>

        </table>
    </div>
</div>


<script>

    $(document).ready(function () {


           // $("#MyDataTable").DataTable();

           BindDataTable();
        })

    var BindDataTable = function (response) {
        var x;


        $("#MyDataTable").DataTable({
            "bServerSide": true,
            "sAjaxSource": "/Home/GetEmployeeRecord",
            "fnServerData": function (sSource, aoData, fnCallback) {               


                $.ajax({

                    type: "Get",
                    data: aoData,
                    url: sSource,
                    success: fnCallback,

                })


                console.log(fnCallback);
            },


            "columns": [

                { "data": "CompanyName" },
                { "data": "Description" },                
                { "data": "Phone1" },
                { "data": "GlobalCompanyID" },

            ]

        });

        console.log(x);


        }


</script>

我的ajax返回对象是这样的

"columns": [

        { "data": 'aaData["CompanyName"]' },
        { "data": 'aaData["Description"]' },
        { "data": 'aaData["Phone1"]'},
        { "data": 'aaData["GlobalCompanyID"]' },


    ],    

我在公共void ConfigureServices(IServiceCollection服务)中将以下代码添加到了startup.cs中,并解决了问题,我猜控制器以前没有返回json格式。更新控制器返回Json

{"sEcho":1,"iTotalDisplayRecords":137,"iTotalRecords":137,"aaData":[{"globalCompanyID":13,"companyTypeName":"Other","companyOwnershipName":"Other","companyIndustryName":"Engineering","parentID":0,"companyName":"ffff","website":"www.youtube.com","eMail":"support@micromarin.com","phone1":"12345688","phone2":"333333333333","fax":"7788554422","description":"Yeni kurulan bir firma","confirmed":true,"active":false},{"globalCompanyID":14,"companyTypeName":"Prospect","companyOwnershipName":"Public","companyIndustryName":"Agriculture","parentID":0,"companyName":"Company 1","website":"www.youtube.com","eMail":"support@micromarin.com","phone1":"12345688","phone2":"333333333333","fax":"7788554422","description":"Yeni kurulan bir firma","confirmed":true,"active":true},{"globalCompanyID":15,"companyTypeName":"Prospect","companyOwnershipName":"Public","companyIndustryName":"Agriculture","parentID":0,"companyName":"Company 2","website":"www.youtube.com","eMail":"support@micromarin.com","phone1":"12345688","phone2":"333333333333","fax":"7788554422","description":"Yeni kurulan bir firma","confirmed":true,"active":true},{"globalCompanyID":16,"companyTypeName":"Prospect","companyOwnershipName":"Public","companyIndustryName":"Agriculture","parentID":0,"companyName":"Company 3","website":"www.youtube.com","eMail":"support@micromarin.com","phone1":"12345688","phone2":"333333333333","fax":"7788554422","description":"Yeni kurulan bir firma","confirmed":true,"active":true},{"globalCompanyID":17,"companyTypeName":"Prospect","companyOwnershipName":"Public","companyIndustryName":"Agriculture","parentID":0,"companyName":"Company 4","website":"www.youtube.com","eMail":"support@micromarin.com","phone1":"12345688","phone2":"333333333333","fax":"7788554422","description":"Yeni kurulan bir firma","confirmed":true,"active":true},{"globalCompanyID":18,"companyTypeName":"Prospect","companyOwnershipName":"Public","companyIndustryName":"Agriculture","parentID":0,"companyName":"Company 5","website":"www.youtube.com","eMail":"support@micromarin.com","phone1":"12345688","phone2":"333333333333","fax":"7788554422","description":"Yeni kurulan bir firma","confirmed":true,"active":true},{"globalCompanyID":19,"companyTypeName":"Prospect","companyOwnershipName":"Public","companyIndustryName":"Agriculture","parentID":0,"companyName":"Company 6","website":"www.youtube.com","eMail":"support@micromarin.com","phone1":"12345688","phone2":"333333333333","fax":"7788554422","description":"Yeni kurulan bir firma","confirmed":true,"active":true},{"globalCompanyID":20,"companyTypeName":"Prospect","companyOwnershipName":"Public","companyIndustryName":"Agriculture","parentID":0,"companyName":"Company 7","website":"www.youtube.com","eMail":"support@micromarin.com","phone1":"12345688","phone2":"333333333333","fax":"7788554422","description":"Yeni kurulan bir firma","confirmed":true,"active":true},{"globalCompanyID":21,"companyTypeName":"Prospect","companyOwnershipName":"Public","companyIndustryName":"Agriculture","parentID":0,"companyName":"Company 8","website":"www.youtube.com","eMail":"support@micromarin.com","phone1":"12345688","phone2":"333333333333","fax":"7788554422","description":"Yeni kurulan bir firma","confirmed":true,"active":true},{"globalCompanyID":22,"companyTypeName":"Prospect","companyOwnershipName":"Public","companyIndustryName":"Agriculture","parentID":0,"companyName":"Company 9","website":"www.youtube.com","eMail":"support@micromarin.com","phone1":"12345688","phone2":"333333333333","fax":"7788554422","description":"Yeni kurulan bir firma","confirmed":true,"active":true}]}

2 个答案:

答案 0 :(得分:0)

这是服务器端数据表的基本结构,

function datatable(sSource, aoData, fnCallback){
    var table = $('#tableID').DataTable({
        dom: dom,
        buttons : [
            'excel', 'pdf', 'print',
        ],
        lengthMenu: [[10, 25, 50, -1], [10, 25, 50, "All"]],
        serverSide: true,
        processing: true,
        ajax: {
            "url": sSource,
            "type": "GET",
            "data": aoData,
            success: fnCallback
        },
    });
}

Ajax请求是GET类型,data_function是,您需要传递任何参数,否则请保留该参数。

希望这会有所帮助!

答案 1 :(得分:0)

我将以下代码添加到startup.cs并解决了问题;

Services.AddMvc().AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver())