I’m writing a binary string from the server like this:
header('Content-type: application/octet-stream');
echo $data = pack('C*', 0, 10, 100, 127, 128, 200, 250, 255, 256);
and reading it with js and jDataView lib this way:
$.get('/get', function(text) {
var view = new jDataView(text);
for (var i = 0; i < 20; i++) {
console.log(i, view.getUint8(i));
}
});
The problem is that I can only read the values that are less than 128. jDataView uses getCharCode at to read Uint and it returns 65533 for each of the bigger values.
How to I get the values in js?
It works just fine when you change the headers sent (by PHP file) with…
header('Content-Type: text/plain; charset=x-user-defined')Without this header (and tweaking of how XHR response should be processed; this article describes the process in details) it’s the
textvalue that becomes messed up: all ‘invalid’ (> 127) characters will be literally replaced by'\uFFFD'ones.Of course, it’s not possible to extract the original values from these characters, so it’s not a bug of jDataView, in my opinion.