In my code, I originally used fetch()
, but for my specific situation, using XMLHttpRequest()
would be more suitable. However, I am struggling to convert the code to utilize XMLHttpRequest()
.
This is the original fetch()
code:
fetch("file.wasm")
.then(response => response.arrayBuffer())
.then(bytes => WebAssembly.instantiate(bytes,importObject))
.then(results => callback(results.instance.exports));
I attempted to rewrite it as follows:
var x;
if(window.XMLHttpRequest)x=new XMLHttpRequest();
else x=new ActiveXObject("Microsoft.XMLHTTP");
x.onreadystatechange=function(){
if(this.readyState!==4)return;
if(this.status>=200&&this.status<300)
callback(WebAssembly.instantiate(x.responseText.arrayBuffer(), importObject).instance.exports);
else return "Error: "+this.status+" "+this.statusText;
}
x.open("GET","file.wasm");
x.send();
The above snippet contains all the necessary code. Alternatively, here is a simple example:
var x = new XMLHttpRequest();
x.onreadystatechange=function(){
if(this.readyState==4 && this.status===200)
callback(WebAssembly.instantiate(x.responseText.arrayBuffer(), importObject).instance.exports);
}
x.open("GET","file.wasm");
x.send();
How can I successfully convert the fetch()
code to use XMLHttpRequest()
instead?
Note: The reason why my XMLHttpRequest
code is not functioning is due to a TypeError
:
Uncaught TypeError: x.responseText.arrayBuffer is not a function
at XMLHttpRequest.x.onreadystatechange (wasm.js)
x.onreadystatechange @ wasm.js
XMLHttpRequest.send (async)
(anonymous) @ wasm.js