Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

210
Visualizações
How to post data to API in jQuery DataTable ajax?

Hi all I am using jQuery DataTable and performing server-side pagination and sorting. I am stuck at one place, I am having API that is of type POST, so I need to send my payload in Body.

I tried POST method in ajax but it kept throwing 415 status code i.e payload format is in an unsupported format. I have noticed that my payload is getting sent in form data. How can I resolve this issue?

Here is my code -

$(document).ready(function () {
    $('#dataTable').DataTable({
        columns: [
            { "data": "FirstName" },
            { "data": "LastName" },
            { "data": "PhoneNumber" },
            { "data": "Address1" },
            { "data": "Email" },
            { "data": "Fax" },
            { "data": "Calls" },
            { "data": "Address2" },
            { "data": "Deal status" },
            { "data": "Conversations" }
        ],
        "processing": true,
        "serverSide": true,
        "ajax":{
            "url": "myAPI",
            "dataType": 'application/json',
            "type": "POST",
            "beforeSend": function(xhr) {
                xhr.setRequestHeader("Authorization", "Bearer Token");
            },
            "data": function (d) {
                d.status= "Active";
                d.field = "";
                d.order="asc";
            },
            dataFilter: function(data) {
                var json = jQuery.parseJSON( data );
                json.recordsTotal = json.totalElements;
                json.recordsFiltered = json.totalElements;
                json.data = json.content;
                console.log("total data",json);
                return JSON.stringify( json ); // return JSON string
            }
        }
    });
});

How can I resolve this 415 status code and send payload correctly? Please Help

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You are non returning anything from the:

"data": function (d) {
    d.status = "Active";
    d.field = "";
    d.order = "asc";
},

so, according to the docs for the ajax.data option:

If there is no return value from the function (i.e. undefined) then the original data object passed into the function by DataTables will be used for the request (the function may have manipulated its values).

If an object is returned, then that object will be used as the data for the request. It will not be merged with the original data object constructed by DataTables before being sent.

Try:

"data": function (d) {
    const extraPayload = {};
    extraPayload.status= "Active";
    extraPayload.field = "";
    extraPayload.order="asc";

    return {...d, ...extraPayload}
},
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda