Note that there are some explanatory texts on larger screens.

plurals
  1. POLosing chars while sending data by JSONP
    primarykey
    data
    text
    <p>I want to create a script (javascript file. JS) that I can put from my domain on every website. The script has to read the text from the website and send it to the server. The server is on another domain than the website from which it reads the text, that is why I use JSONP. The read text can be charsed in different ways (different languages – websites from all over the world) and they can be very long that’s why, before sending them I code them by base64, divide them into data packets and send them separately using GET (JSONP).</p> <p>Without the definition of <code>getJson()</code> function, the code is:</p> <pre><code>var sBase64Code = base64_encode( document.getElementById('idText').innerHTML ); // we devide sBase64Code into data packets, // and changing it to array aBase64Code – I skip this algorythm // and then I send packets to the server in loops for(i in aBase64Code) { getJson(['idx='+i, 'code='+aBase64Code[i]], hFunCallback); } </code></pre> <p>However, after sending the code to the server, joining the packets and decoding data by <code>base64_decode()</code> it turns out that chars different from English ones are lost.</p> <p><code>mb_convert_encoding(base64_decode($b64), mb_detect_encoding(base64_decode($b64)))</code> doesn’t work.</p> <p>JavaScript <code>base64_encode()</code> function is:</p> <pre><code>function base64_encode(data) { var b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; var o1, o2, o3, h1, h2, h3, h4, bits, i = 0, ac = 0, enc = "", tmp_arr = []; if(!data) return data; do { o1 = data.charCodeAt(i++); o2 = data.charCodeAt(i++); o3 = data.charCodeAt(i++); bits = o1 &lt;&lt; 16 | o2 &lt;&lt; 8 | o3; h1 = bits &gt;&gt; 18 &amp; 0x3f; h2 = bits &gt;&gt; 12 &amp; 0x3f; h3 = bits &gt;&gt; 6 &amp; 0x3f; h4 = bits &amp; 0x3f; tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4); } while (i &lt; data.length); enc = tmp_arr.join(''); var r = data.length % 3; return (r ? enc.slice(0, r - 3) : enc) + '==='.slice(r || 3); }; </code></pre> <p>Example of sending data: <img src="https://i.stack.imgur.com/J1gjB.jpg" alt="enter image description here"></p> <p>Base64 from example: <code>WndpbmkZdGEgbmkHIHRvIGtCGWJlay4=</code></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload