weixin_33735077 2014-11-16 10:20 采纳率: 0%
浏览 67

用PHP压缩JSON响应

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

  • 写回答

2条回答 默认 最新

  • weixin_33696822 2014-11-16 10:24
    关注

    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.

    评论

报告相同问题?