Skip to content

Instantly share code, notes, and snippets.

@davidsharp
Created April 22, 2022 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidsharp/60f9300494731058eae3deaebbff9da2 to your computer and use it in GitHub Desktop.
Save davidsharp/60f9300494731058eae3deaebbff9da2 to your computer and use it in GitHub Desktop.
Proxying a XMLHttpRequest to force the status received
XMLHttpRequest = new Proxy(XMLHttpRequest, {
construct:function(t,a){
const req = new t();
return new Proxy(req, {
get:function(o,p){
if(p=='status')return 9001
return typeof o[p] == 'function'?o[p].bind(o):o[p]
},
set: function(target, prop, value) {
Reflect.set(target, prop, value) // or target[prop] = value
return true;
},
})
}
})
var oReq = new XMLHttpRequest();
oReq.addEventListener("load", transferComplete);
oReq.addEventListener("error", transferComplete);
oReq.open("GET", "https://blossom-emery.glitch.me/yo.json");
oReq.responseType = "json";
oReq.send()
console.log(oReq)
function transferComplete(evt) {
console.log(evt);
console.log(oReq.status,oReq.response);
}
@davidsharp
Copy link
Author

I needed this for some reason a couple of years back, and only just got the SO answer that got this properly working recently

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment