Hello,
We move your request to the dev team to write a new article for the cookbook (https://www.phpdocx.com/documentation/cookbook/).
If you get a generic error such as 'Unexpected token P in JSON at position 0' is because the stream output is not being handled correctly by your JS, for example, it's not being readed as a binary content. Maybe the easiest solution for your project would be using a library such as jquery-binarytransport (https://github.com/henrya/js-jquery/tree/master/BinaryTransport).
Regards.