当AJAX请求使用GET方法时,我有一些代码可以很好地工作,但我需要将其更改为POST方法,以避免出现以下错误:"400Bad RequestLine is Too Large"

因此,当我将get方法更改为post时,我在控制台中得到以下错误:

"未捕获的类型错误:无法在字符串"{"draw":1,"columns":[{"data":null,"name":"","searchable":false,"orderable":false,"search":{"value":"","regex":false}},."上创建属性"searchPanes"."

这是我的代码:

const table = $('#datatables').DataTable({
    serverSide: true,
    responsive: false,
    scrollX: true,
    destroy: true,
    deferRender: true,
    select: true,
    processing: true,
    searchPanes: {
        cascadePanes: true,
        viewTotal: true,
        layout: 'columns-3',
    },
    pageLength: 20,
    language: {
        url: '//cdn.datatables.net/plug-ins/1.10.13/i18n/Spanish.json',
        searchPlaceholder: "Producto | Laboratorio",
        search: "<span class='fw-bold'>Buscar:</span>",
        searchPanes: {
            title: {
                _: 'Filtros seleccionados - %d',
                0: 'No hay filtros seleccionados',
                1: 'Un filtro seleccionado'
            },
            collapseMessage: 'Ocultar filtros',
            showMessage: 'Mostrar filtros',
            clearMessage: 'Limpiar todos los filtros'
        }
    },
    ajax: {
        url: `${window.location.origin}/dashboard/products/list/`,
        type: "POST",
        contentType: 'application/json',
        headers: {'Authorization': `Token ${authToken}`},
        data: function (d) {
            return JSON.stringify(d);
        },
    },
    columns: columns,
    order: [[1, 'asc']],
    dom: "PfBrtip",
    columnDefs: [{
        searchPanes: {
            show: true, initCollapsed: true, dtOpts: {
                dom: "tp",
                paging: true,
                pagingType: 'numbers',
                pageLength: 5,
                autoWidth: true,
                scrollX: true,
                scrollY: true,
                scrollCollapse: true,
            }
        }, targets: [3, 4, 5, 6, 7, 8],
    },
        {
            targets: 3,
            visible: false,
        },
        {
            targets: 4,
            visible: false,
        },
        {
            targets: 5,
            visible: false,
        },
        {
            targets: 6,
            visible: false,
        },
        {
            targets: 7,
            visible: false,
        },
        {
            targets: 8,
            visible: false,
        },
        {
            targets: Array.from({length: drugstoreQuantity * 3}, (_, index) => 9 + index), className: 'text-center'
        }
    ],
    buttons: {
        // Default classes for buttons
        dom: {
            button: {
                tag: 'button', className: 'btn btn-xs',
            }
        }, // Actual buttons
        buttons: [{
            action: () => toggleConversion(),
            className: "btn btn-success m-1 text-2",
            text: '<i class="fa fa-coins"></i> &nbsp; Convertir <strong>Dólares-Bolívares</strong>',
        }, {
            className: "btn btn-primary m-1 text-2",
            text: 'Ocultar/Mostrar <strong>Días de crédito</strong>',
            action: function (e, dt) {
                const columnsToToggle = [11, 14, 17, 20];
                columnsToToggle.forEach((columnIndex) => {
                    const currentVisibility = columnVisibility[columnIndex];
                    dt.column(columnIndex).visible(!currentVisibility);
                    columnVisibility[columnIndex] = !currentVisibility;
                });
            },
        },],
    },
});

似乎问题出在AJAX请求内的数据参数中,这里

    ajax: {
        url: `${window.location.origin}/dashboard/products/list/`,
        type: "POST",
        contentType: 'application/json',
        headers: {'Authorization': `Token ${authToken}`},
        data: function (d) {
            return JSON.stringify(d);
        },
    },

似乎我不能在将数据发送到服务器之前对数据进行字符串化,但是如果我不使用JSON.stringify,服务器就不会收到post请求的数据.

有什么帮助吗?请

推荐答案

在将数据发送到服务器之前,我似乎无法将其串行化

ajax: {
    url: ...
    type: 'POST',
    ...    

数据表的ajax:属性与jQuery的$.ajax()属性不同--它是数据表的一部分,因此您在这里不是"将其发送到服务器",而是将其发送到Dataables以添加其部分,然后Dataables将其发送到服务器.

解决方案是提供对ajax:属性的函数回调-the datatables documentation中有更多信息:

function ajax( data, callback, settings )

As a function, making the Ajax call is left up to yourself allowing complete control of the Ajax request

Jquery相关问答推荐

如何使用 JQuery 将详细信息中的项目包装在容器中?

将 jquery 元素转换为 html 元素

无法使用 HTML 设置未定义的 jQuery UI 自动完成的属性_renderItem

在学习 jQuery 之前学习 JavaScript 是个好主意吗?

document.querySelector 一个元素中的多个数据属性

jQuery - 向下滚动时缩小的粘性标题

如何在使用 jQuery 单击特定链接时打开 bootstrap 导航选项卡的特定选项卡?

如何使用jQuery找到元素顶部的垂直距离(以px为单位)

jQuery Select 一个具有某个类的div,它没有另一个类

JSON 到字符串变量转储

如何重新启用 event.preventDefault?

如何将文本从 div 复制到剪贴板

触发下拉更改事件

jQuery 和 jQuery Mobile 的区别?

如何从所有元素jquery中删除类

通过单击按钮获取表格行的内容

jQuery select2 获取 Select 标签的值?

如何使用 jquery 使下拉列表只读?

如何禁用由子元素触发的 mouseout 事件?

jQuery .val 更改不会更改输入值