简体   繁体   English

如何将jQuery DataTable ajax中的数据post到API?

[英]How to post data to API in jQuery DataTable ajax?

Hi all I am using jQuery DataTable and performing server-side pagination and sorting.大家好,我正在使用 jQuery DataTable 并执行服务器端分页和排序。 I am stuck at one place, I am having API that is of type POST, so I need to send my payload in Body.我被困在一个地方,我有 API 类型的 POST,所以我需要在 Body 中发送我的有效负载。

I tried POST method in ajax but it kept throwing 415 status code ie payload format is in an unsupported format .我在 ajax 中尝试了POST方法,但它一直抛出415状态代码,即有效负载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?如何解决此 415 状态代码并正确发送有效负载? Please Help请帮忙

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:因此,根据ajax.data选项的文档

If there is no return value from the function (ie 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).如果 function 没有返回值(即undefined ),则DataTables 传递到 function 的原始数据 object 将用于请求(function 可能操纵了它的值)。

If an object is returned, then that object will be used as the data for the request.如果返回 object,则该 object 将用作请求的数据。 It will not be merged with the original data object constructed by DataTables before being sent.发送前不会与DataTables构造的原始数据object合并。

Try:尝试:

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

    return {...d, ...extraPayload}
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM