Wrong payload ajax
Wrong payload ajax
Hi,
i have a problem using datatable with an ajax source. The ajax request contain the wrong payload.
I use datatables in version 1.10.12.
I can do the following standalone ajax request:
var getRequest ={"id":-1,"filterName":"assetFilter"};
getRequest = JSON.stringify(getRequest);
$.ajax({
type : 'POST',
url : "http://"+restServer+":"+restPort+"/asset/get",
data : getRequest,
dataType : "json",
contentType : "application/json; charset=utf-8"});
This is a valid call in my environment. I see in the debugger of the browser that everything is fine. The payload of the request is:
{id: -1, filterName: "assetFilter"}
That fits to my expecation.
I get a different payload if i use the following code in my table definition:
ajax :{
type : 'POST',
url : "http://"+restServer+":"+restPort+"/asset/get",
data : getRequest,
dataType : "json",
contentType : "application/json; charset=utf-8"}
For my understanding, the code should do exactly the same as the separate ajax call before. But the payload of the request is:
0=%7B&1=%22&2=i&3=d&4=%22&5=%3A&6=-&7=1&8=%2C&9=%22&10=f&11=i&12=l&13=t&14=e&15=r&16=N&17=a&18=m&19=e&20=%22&21=%3A&22=%22&23=a&24=s&25=s&26=e&27=t&28=F&29=i&30=l&31=t&32=e&33=r&34=%22&35=%7D
The var getRequest is not changed in between.
Can someone explain what happens here?
Replies
Can you link to the page showing the issue so I can attempt to debug it please?
Allan
It is just on my laptop? Could i give you some more information?
I have seen, that i have used an old jquery. But now i replaced it with 3.1.0. The issue is the same.
Here is a screenshot from the debugger. I have seen, that the ajax call will be executed in row 3930.
baseajax contains at this point in time an array. The array contain my expected payload splitted up in separate characters.
baseajax contain GET as type but my request was a POST.
Is this the expected content for those variables?
My ajax call is successfull if i do this after the initialization of baseAjax
This is of course just a dirty fix and later on i get an exception. I dit it just for testing.
Looks as the goes something wrong with the data?
Or do i use it wrong?
Are you able to use JSFiddle, CodePen, http://live.datatables.net or similar to create an example showing the issue please?
I assume
getRequest
is an object? Or is it a string?Allan
Ok, i created a jsfiddle:
https://jsfiddle.net/2a1a7num/15/
The issue is the same. The jquery rest call works fine. The call from the datatable has an invalid payload.
Awesome - thanks!
So the issue is that
ajax.data
cannot be a string for DataTables. Instead, if you want to post JSON data in the request body you need to do the same as the last example on that reference page and return the string fromajax.data
as a function - https://jsfiddle.net/2a1a7num/16/ .Why? Because sometimes DataTables needs to send its own data parameters to the server (server-side processing). Merging with a string would be possible if it was certain the string was JSON, but that doesn't need to be the case.
Allan
Ok, understood. I will try to include it into my app.
Thx for your effort, was very helpfull