Skip to content
Advertisement

AJAX – Convert returned octet-stream to typed array (Float64Array)

I cannot figure out what I’m doing wrong here. I’m trying to convert a binary stream, returned from an AJAX call, to an array of doubles in JavaScript. Some code: My server PHP returns an octet-stream (array of doubles):

JavaScript

In my webpage I have an AJAX call:

JavaScript

And then my function for processing the returned data from the rest call doSomething(data):

JavaScript

The issue I’m facing is that Float64Array doesn’t seem to be converting my data to an array. I get a size of zero and undefined while count is a large number. There are no console errors in Chrome so I’m having a hard time really pinning down what I’m missing. Am I suppose to convert data to an ArrayBuffer first? I have looked at data in a hex editor and confirmed the returned byte stream is the correct array of doubles (64-bit little endian) with correct values.

Advertisement

Answer

The Float64Array constructor expects an ArrayBuffer argument. In order to have the browser interpret the response as such, try

JavaScript

The fetch API equivalent would be like this, using the Response.arrayBuffer() method

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement