0

Consider I have compressed data follow :

Ả㠵堥䂢桥ƴېୠ⤡

I am sending these kind of chars by follow through Java script/jquery:

function callServer()
{
    var compressed = LZString.compressToUTF16(uncompressed64Data); 

    jQuery.ajax({
        url : "/RegisterServlet_2/servlet/Register",
        type : "POST",
        dataType: "json",
         data: {
                 img : compressed,
            },
        cache : false,
        async : false,
        success : function()
        {
        },
        error : function()
        {
        }
    });
}

While sending this by automatic the data encoded due to special chars and request body look like this : %E1%BA%A2%E3%A0%B5%E5%A0%A5%E4%82%A2%E6%A1%A5%C6%B4%DB%90%E0%AD

My objective is I want to reduce the bandwidth weight, Hence I was used compression algorithm and it was reduced much of chars. (Please note, I am given here is sample but the data I am using is huge!)

Hence I want to send the special chars just as it is without any encode algorithm or even if I allow the chars count size should not increase after encoding? Any help much appreciated. Thanks in advance

Will Mcavoy
  • 485
  • 5
  • 24
  • By default jQuery uses a `contentType` of `'application/x-www-form-urlencoded; charset=UTF-8'`. You need to change the `charset` to whatever you want to use, although I personally don't see any issue with the encoding as it is. Concern over using 20 extra bytes seems like a massive over-optimisation to me. – Rory McCrossan Sep 19 '17 at 14:46
  • @RoryMcCrossan : I am processing 579048 chars but once I do compression 150770 with special chars. This data has to go server. I am bit confused, please clarify me, don't you think it is better optimization? – Will Mcavoy Sep 19 '17 at 14:52

1 Answers1

0

If size matters, you'd better strip out all consecutive white-space (this can safely be done, unless the HTML contains a <pre> element/style). Then, only replace the significant characters:

var html = document.getElementById("html").innerHTML;
html = html.replace(/\s{2,}/g, '')   // <-- Replace all consecutive spaces, 2+
           .replace(/%/g, '%25')     // <-- Escape %
           .replace(/&/g, '%26')     // <-- Escape &
           .replace(/#/g, '%23')     // <-- Escape #
           .replace(/"/g, '%22')     // <-- Escape "
           .replace(/'/g, '%27');    // <-- Escape ' (to be 100% safe)
var dataURI = 'data:text/html;charset=UTF-8,' + html;

https://stackoverflow.com/a/9239272/209942

johny why
  • 2,047
  • 7
  • 27
  • 52