jQuery datatable AJAX 附加不需要的查询参数

jquery datatable ajax appending unwanted query params

提问人:Bopsi 提问时间:9/14/2020 更新时间:9/14/2020 访问量:397

问:

我正在使用服务器端分页实现jquery DataTable。请参阅下面的代码片段 -

脚本 Html
$(async function() {
    $('#registry_table').DataTable({
        serverSide: true,
        ajax: {
            url: 'localhost:3000/provenance/registries',
            headers: {
                'Content-Type': 'application/json',
                'Authorization': 'Bearer ' + localStorage.getItem("token")
            },
            params: {
                page: 0,
                per_page: 5
            }
        },
        language: {
            searchPlaceholder: "Search...",
            search: "",
            lengthMenu: "_MENU_  items/page"
        }
    });
});
<table class="table table-striped mg-b-0" id="registry_table">
    <thead>
        <tr>
            <th class="text-left">#</th>
            <th class="text-left">Name</th>
            <th class="text-left">Created on</th>
            <th class="text-left">Transaction</th>
            <th class="text-center">Action</th>
        </tr>
    </thead>
</table>

但是,当调用 REST API 时,它会生成不正确的 URL

http://localhost:3000/provenance/registries?draw=1&columns%5B0%5D%5Bdata%5D=0&columns%5B0%5D%5Bname%5D=&columns%5B0%5D%5Bsearchable%5D=true&columns%5B0%5D%5Borderable%5D=true&columns%5B0%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B0%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B1%5D%5Bdata%5D=1&columns%5B1%5D%5Bname%5D=&columns%5B1%5D%5Bsearchable%5D=true&columns%5B1%5D%5Borderable%5D=true&columns%5B1%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B1%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B2%5D%5Bdata%5D=2&columns%5B2%5D%5Bname%5D=&columns%5B2%5D%5Bsearchable%5D=true&columns%5B2%5D%5Borderable%5D=true&columns%5B2%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B2%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B3%5D%5Bdata%5D=3&columns%5B3%5D%5Bname%5D=&columns%5B3%5D%5Bsearchable%5D=true&columns%5B3%5D%5Borderable%5D=true&columns%5B3%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B3%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B4%5D%5Bdata%5D=4&columns%5B4%5D%5Bname%5D=&columns%5B4%5D%5Bsearchable%5D=true&columns%5B4%5D%5Borderable%5D=true&columns%5B4%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B4%5D%5Bsearch%5D%5Bregex%5D=false&order%5B0%5D%5Bcolumn%5D=0&order%5B0%5D%5Bdir%5D=asc&start=0&length=10&search%5Bvalue%5D=&search%5Bregex%5D=false&_=1600077609184

如您所见,它没有传递页面和per_page查询参数。并且不确定所有其他参数是如何添加到URL的。

DataTable 分页服务器 查询字符串

评论


答:

0赞 Bopsi 9/14/2020 #1

将键更改为并返回我想以 JSON 形式使用 URL 发送的参数。paramsdata

更新的脚本:它删除了所有不需要的标头预期
ajax: {
    url: `${self.$provenance.defaults.baseURL}/provenance/registries`,
    headers: {
        'Content-Type': 'application/json',
        'Authorization': 'BWS ' + localStorage.getItem("token")
    },
    data: () => {
        return {page: 0, per_page: 5};
    },
    dataSrc: 'registries'
}
_
http://localhost:3000/provenance/registries?page=0&per_page=5&_=1600082235692