LimeStreem LimeStreem - 9 days ago 5
Ajax Question

Why Javascript XMLHttpRequest octet-stream MIME type returns the response as string?

I tried to obtain gltf binary file with

XMLHttpRequest
. This is the code I tried.

var xhr = new XMLHttpRequest();
xhr.open("GET","THE ADDRESS",true);
xhr.setRequestHeader("Accept", "application/octet-stream");
xhr.responseType = "arraybuffer";
xhr.onload = function (v) {
resolve(xhr.response); // xhr.response must be ArrayBuffer but xhr.response is string
};
xhr.onerror = function (e) {
reject(e);
};
xhr.send();


As per the code above, I definitely set responseType as
arraybuffer
. But when the onload handler called,
responseType
field was changed as
""
.

I'm not calling any other ``XMLHttpRequest in this code. This is response header I got.

accept-ranges:bytes
content-length:102040
content-type:application/octet-stream
date:Fri, 18 Nov 2016 09:30:13 GMT
etag:"582ec3ef-18e98"
last-modified:Fri, 18 Nov 2016 09:03:43 GMT
server:h2o/2.0.4
status:200


And this is the request header I sent.

:authority:localhost
:method:GET
:path:/gex/t90-01/Duck/Duck.bin
:scheme:https
accept:*/*
accept-encoding:gzip, deflate, sdch, br
accept-language:ja,en-US;q=0.8,en;q=0.6
cache-control:no-cache
pragma:no-cache
referer:https://localhost/gex/
user-agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36


Anyt help?

Answer

You can try the below code by changing setRequestHeader with overrideMimeType as below.

And also, you should handle the xhr.response properly inside the resolve method you used below and not sure, you have done it correctly.

var xhr = new XMLHttpRequest();
xhr.open("GET","THE ADDRESS",true);
xhr.overrideMimeType("application/octet-stream");
//xhr.setRequestHeader("Accept", "application/octet-stream");
xhr.responseType = "arraybuffer";
xhr.onload = function (v) {
    resolve(xhr.response); // xhr.response must be ArrayBuffer but xhr.response is string
};
xhr.onerror = function (e) {
    reject(e);
};
xhr.send();
Comments