Decrypt response before processing in datatable.
Decrypt response before processing in datatable.
sabareesh_pix
Posts: 3Questions: 1Answers: 0
I am receiving response from API as encrypted and I have the decryption function available in my JS as well. So, before processing response in datatable, I want to decrypt the response. So where should I put the logic?
I have tried with dataSrc
attribute, but there I can only decrypt and provide data as return. Other values such as recordsTotal, recordsFiltered is not passing on to table. So where should I implement this?
dataSrc: function(json) {
let decrypted = decryptData(json);
if (decrypted != null && decrypted != "") {
json = JSON.parse(decrypted);
}
json.recordsTotal = json.recordsTotal
json.recordsFiltered = json.recordsFiltered
return json.data || [];
}
Answers
Since 2.0 the
ajax.dataSrc
option can be given as an object to allow it to work withdata
,draw
and the other properties. You need to specify a function for each one.The alternative, and it might be best in this case, is to specify
ajax
as a function - make your own Ajax call, decode the result and then pass it to the callback function.Allan
I was able to resolved the issue by doing:
But, what I don't understand is that.. It is not working when I replace the whole response with decrypted data as below. I have no any additional keys with my response. Only
data
,recordsTotal
,recordsFiltered
. I like to know what is the reason though.I don't actually see any difference between the two code blocks? Most likely though the issue was that
dataSrc
expects an array on return, not the object withrecordsTotal
etc.Allan
Apologies, I mistakenly pasted the same code samples above, so please disregard them.
When I set dataSrc as shown below, it doesn't include
recordsTotal
andrecordsFiltered
:However, if I don't completely override the JSON object but instead replace only specific properties, like
recordsTotal
andrecordsFiltered
, it works fine. Here’s an example where I override only these properties without fully replacing the JSON:This assigned a different object to the local
json
variable. It does not effect the object that had been passed in and assigned to the localjson
variable.The reason why
json.recordsTotal = decryptedData.recordsTotal || 0
works, is that you are mutating the original object, not just a local variable pointer.Allan