How to make Editor send a json object in the remove request

How to make Editor send a json object in the remove request

clorkclork Posts: 1Questions: 1Answers: 0

Hello, I implement interface for my small CRUD app and would like to send a json in all create, edit and remove requests. Json is sent at create and edit, but data is sent in delete as query parameters.

My js file:

var editor;
var table;

const getDataCreateEdit = function () {
    let data = $.extend(editor.val(), {entity:'author'});

    return JSON.stringify(data);
};

const getDataRemove = function (obj) {
    let id = Object.keys(obj.data);

    if (id.length > 1) throw 'To many ids';

    let data = {
        entity: 'author',
        id: Number(id[0])
    };

    return JSON.stringify(data);
};

$(document).ready(function() {
    editor = new $.fn.dataTable.Editor( {
        ajax: {
            create: {
                type: 'PUT',
                url: '/api/v1/entity/author',
                contentType: 'application/json',
                dataType: 'json',
                data: getDataCreateEdit
            },
            edit: {
                type: 'PATCH',
                url: '/api/v1/entity/author',
                contentType: 'application/json',
                dataType: 'json',
                data: getDataCreateEdit
            },
            remove: {
                type: 'DELETE',
                url: '/api/v1/entity/author',
                contentType: 'application/json',
                dataType: 'json',
                data: getDataRemove
            }
        },
        table: "#authors",
        idSrc: 'id',
        fields: [
            {
                label: "Second Name:",
                name: "second_name"
            }, {
                label: "First Name:",
                name: "first_name"
            }, {
                label: "Patronymic:",
                name: "patronymic"
            }, {
                label: "Date of Birth:",
                name: "date_of_birth",
                type: "datetime"
            }, {
                label: "Country:",
                name: "country"
            }, {
                label: "Degree:",
                name: "degree"
            }
        ]
    } );


    table = $('#authors').DataTable({
        dom: 'Bfrtip',
        orderMulti: true,
        scrollY: 600,
        scrollCollapse: true,
        paging: false,
        ajax: {
            url: '/api/v1/entity/author',
            dataSrc: 'entities'
        },
        columns: [
            { data: 'second_name' },
            { data: 'first_name' },
            { data: 'patronymic' },
            { data: 'date_of_birth' },
            { data: 'country' },
            { data: 'degree' }
        ],
        select: true,
        buttons: [
            { extend: 'create', editor: editor },
            { extend: 'edit', editor: editor },
            { extend: 'remove', editor: editor }
        ]

    });
});

And this is how my request parameters look like

!

Answers

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    The ajax.deleteBody option can be used for this. The documentation also explains why this is the case :).

    Allan

This discussion has been closed.