I am using the following little gem within the background script of a Mozilla webextension:
function makeRequest(url) {
return new Promise(function(resolve,reject) {
console.log("begin makeRequest");
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onload = function() {
if (this.status >= 200 && this.status < 300) {
console.log("resolve makeRequest");
resolve(xhr.response);
} else {
console.log("reject makeRequest");
reject({
status: this.status,
statusText: xhr.statusText
});
}
}
xhr.onerror = function() {
console.log("reject makeRequest by error");
reject({
status: this.status,
statusText: xhr.statusText
});
}
xhr.send(null);
console.log("makeRequest sent");
});
}
I invoke this with a line like makeRequest("http://www.example.com")
and I can verify from the Apache access log at www.example.com, that the request succeeds (status code "200"). In particular, this makes me confident that the request does not fail due to some misisng permissions in the manifest or the like. Consequently, I'd expect the xhr.onload
to be invoked with this.status == 200
.
But instead of this, the xhr.onerror
is invoked, i.e., the console log reads
begin makeRequest
makeRequest sent
reject makeRequest by error
Why? (As in : How is this even possible?)