当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> 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请求的数据.
有什么帮助吗?请