Decrypt response before processing in datatable.
Decrypt response before processing in datatable.
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 || [];
}
This discussion has been closed.
Answers
Since 2.0 the
ajax.dataSrcoption can be given as an object to allow it to work withdata,drawand 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
ajaxas 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
dataSrcexpects an array on return, not the object withrecordsTotaletc.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
recordsTotalandrecordsFiltered:However, if I don't completely override the JSON object but instead replace only specific properties, like
recordsTotalandrecordsFiltered, 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
jsonvariable. It does not effect the object that had been passed in and assigned to the localjsonvariable.The reason why
json.recordsTotal = decryptedData.recordsTotal || 0works, is that you are mutating the original object, not just a local variable pointer.Allan