Loading an Image with AJAX


I’m trying to get the raw bytes of an image using AJAX, base64 encode them using JavaScript, and then load them using a data uri, but something is going wrong. The response from the AJAX request doesn’t seem to match up with the contents of the file that is being served. AJAX is giving me back a string that has unicode characters, but when I do string.charCodeAt(…) the bytes that I’m getting out don’t seem to be the same as the ones in the file. An example the first 4 bytes in the file are (hex)


and the first two characters of the string have character codes (binary)

65533 80

Is there something that I’m doing wrong? Is there a way to get the raw response as an array of bytes rather than a string? The response renders fine if I just visit the page directly.

Any help would be greatly appreciated. Thanks.