0

I've a simple MVC model. I'm doing an Ajax request where I send some data to be processed by PHP and retrieve database records as JSON. As this object could be quite large, is there some way I could compress/encrypt it on the PHP (server side) and decrypt it on the Javascript side (client)

$.ajax({
    url: "/php/function/link/",
    dataType: 'json',
    data: {
            "date": date,
          },
    type: "POST", 
    success: function(_data){
              // load encrypted data here and decrypt it.
          },
    error: function() {
       alert("Some error fetching!");

    }

I tried using the following methods, but they didn't seem to work (I was getting error while decompressing them on the javascript end):

  1. JSONC
  2. https://stackoverflow.com/a/11901649/1443702

Are there any other better ways? I simply need to :

compress data on javascript to be passed from client->send it to server (PHP) -> decompress it and compute database queries -> compress it -> pass it to javascript(client side) -> decompress it

Community
  • 1
  • 1
u134211
  • 315
  • 1
  • 2
  • 8

2 Answers2

0

The best way is just to enable HTTP traffic compression in web server. All modern browsers and servers support it. Read more about HTTP_compression. And you will have additional bonus: all your traffic will be compressed, not AJAX traffic only.

Valera Leontyev
  • 1,191
  • 6
  • 14
0

For php - You can try this: http://rosettacode.org/wiki/LZW_compression#PHP

For JavaScript - http://rosettacode.org/wiki/LZW_compression#JavaScript

Ideally you should avoid sending large data set unless the use case is unavoidable. I would suggest you to reconsider your design. But recently I ran into similar use-case as part of product requirement where I needed to compress handle 5MB of JSON data (partially) in JavaScript. I tried the above and was able achieve 50% compression.

Prabhakar Kasi
  • 531
  • 4
  • 18