Decrypt response before processing in datatable.

Decrypt response before processing in datatable.

sabareesh_pixsabareesh_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

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Since 2.0 the ajax.dataSrc option can be given as an object to allow it to work with data, 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

  • sabareesh_pixsabareesh_pix Posts: 3Questions: 1Answers: 0

    I was able to resolved the issue by doing:

    dataSrc: function(json) {
        const decrypted = decryptData(json);
        if (decrypted != null && decrypted != "") {
            const decryptedData = JSON.parse(decrypted);
            json.recordsTotal = decryptedData.recordsTotal || 0
            json.recordsFiltered = decryptedData.recordsFiltered || 0
            json.data = decryptedData.data || [];
        }
        return json.data;
    }
    

    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.

    dataSrc: function(json) {
        const decrypted = decryptData(json);
        if (decrypted != null && decrypted != "") {
            const decryptedData = JSON.parse(decrypted);
            json.recordsTotal = decryptedData.recordsTotal || 0
            json.recordsFiltered = decryptedData.recordsFiltered || 0
            json.data = decryptedData.data || [];
        }
        return json.data;
    }
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    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 with recordsTotal etc.

    Allan

  • sabareesh_pixsabareesh_pix Posts: 3Questions: 1Answers: 0

    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 and recordsFiltered:

    dataSrc: function(json) {
        const decrypted = decryptData(json);
        if (decrypted != null && decrypted != "") {
            json = JSON.parse(decrypted);
        }
        return json.data;
    }
    

    However, if I don't completely override the JSON object but instead replace only specific properties, like recordsTotal and recordsFiltered, it works fine. Here’s an example where I override only these properties without fully replacing the JSON:

    dataSrc: function(json) {
        const decrypted = decryptData(json);
        if (decrypted != null && decrypted != "") {
            const decryptedData = JSON.parse(decrypted);
            json.recordsTotal = decryptedData.recordsTotal || 0
            json.recordsFiltered = decryptedData.recordsFiltered || 0
            json.data = decryptedData.data || [];
        }
        return json.data;
    }
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    json = JSON.parse(decrypted);
    

    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 local json 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

Sign In or Register to comment.