如何post数据到jQuery数据表ajax中的API?
How to post data to API in jQuery DataTable ajax?
大家好,我正在使用 jQuery DataTable 并执行服务器端分页和排序。
我被困在一个地方,我有 API 类型 POST,所以我需要在 Body 中发送我的有效载荷。
我在 ajax 中尝试了 POST
方法,但它一直抛出 415
状态代码,即负载 format is in an unsupported format
。我注意到我的有效负载以表单数据的形式发送。
我该如何解决这个问题?
这是我的代码 -
$(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
}
}
});
});
如何解决这个 415 状态码并正确发送负载?
请帮忙
您不会从以下位置退回任何东西:
"data": function (d) {
d.status = "Active";
d.field = "";
d.order = "asc";
},
所以,根据 docs 的 ajax.data
选项:
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.
尝试:
"data": function (d) {
const extraPayload = {};
extraPayload.status= "Active";
extraPayload.field = "";
extraPayload.order="asc";
return {...d, ...extraPayload}
},
大家好,我正在使用 jQuery DataTable 并执行服务器端分页和排序。 我被困在一个地方,我有 API 类型 POST,所以我需要在 Body 中发送我的有效载荷。
我在 ajax 中尝试了 POST
方法,但它一直抛出 415
状态代码,即负载 format is in an unsupported format
。我注意到我的有效负载以表单数据的形式发送。
我该如何解决这个问题?
这是我的代码 -
$(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
}
}
});
});
如何解决这个 415 状态码并正确发送负载? 请帮忙
您不会从以下位置退回任何东西:
"data": function (d) {
d.status = "Active";
d.field = "";
d.order = "asc";
},
所以,根据 docs 的 ajax.data
选项:
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.
尝试:
"data": function (d) {
const extraPayload = {};
extraPayload.status= "Active";
extraPayload.field = "";
extraPayload.order="asc";
return {...d, ...extraPayload}
},