首页 > 解决方案 > DataTables 警告:使用 Json 请求未知参数“FileName”...

问题描述

我有一个加载脚本的视图:

var dataTable;

$(document).ready(function () {
    loadDataTable();
});

function loadDataTable() {
    dataTable = $('#DT_load').DataTable({
        "ajax": {
            "url": "/musicfiles/getall/",
            "type": "GET",
            "datatype": "json"
        },
        "columns": [
            {
                "data": "albumartpath",
                "render": function (data) {
                    return `<img src="~/images/" + ${data} ?? "noimage.webp" asp-append-version="true" height="50" width="50" />`;
                }, "width": "20%"
            },
            { "data": "filename", "width": "20%" },
            { "data": "title", "width": "20%" },
            { "data": "artist", "width": "20%" },
            { "data": "genre", "width": "20" }
        ],
        "language": {
            "emptyTable": "no data found"
        },
        "width": "100%"
    });
}

和返回 Json 的 MusicFilesController 函数:

[HttpGet]
public async Task<IActionResult> GetAll()
{
    JsonResult json = Json(new { data = await _context.MusicFiles.ToListAsync() });
    return json;
}

但是当我加载页面时,我得到了错误:

DataTables 警告:表 id=DT_load - 请求第 0 行第 1 列的未知参数“文件名”。有关此错误的更多信息,请参阅http://datatables.net/tn/4

Json 使用字符串正确格式化,但我无法弄清楚我缺少什么。我浏览了大约十几个其他有类似问题的帖子,但没有找到答案。

标签: ajaxasp.net-coredatatablesasp.net-core-mvc

解决方案


请求第 0 行第 1 列的未知参数“文件名”。

在您的代码中,我们可以发现您使用 指定columns.data了选项{ "data": "filename", "width": "20%" },如果数据源对象项的键fileName如下所示(不是filename您指定的),这将导致问题。

{"albumartpath":"https://xxxx/xxx","fileName":"xxx","title":"xxx","artist":"xxx","genre":"xx"}

因此,请仔细检查客户端收到的数据,并确保正确设置列的数据源。


推荐阅读