I'm trying to make a cross-origin XMLHttpRequest
from within a web worker. The setup is as follows:
example.com
s3.amazon.com
Access-Control-Allow-Origin
headerThe code is as follows:
var xhr = new XMLHttpRequest();
//this will redirect to 'https://s3.amazon.com/...'
xhr.open('GET', 'https://example.com/document/1234/download');
xhr.send(null);
This code properly follows the redirect, both when called from the main JS file and from a web worker.
This code properly follows the redirect only when called from the main JS file. When called from a web worker, the request is aborted without an error or log message.
It's possible to make this work by editing the browser security settings and enabling "Access data sources across domains," but it is not viable to expect users to do so.
There are several possible work-arounds to this problem, two of which I explored:
function callXHR() {
var xhr = new XMLHttpRequest();
//this will redirect to 'https://s3.amazon.com/...'
xhr.open('GET', 'https://example.com/document/1234/download');
xhr.send(null);
}
if(isIE) {
//callXHR in main js file
} else {
//callXHR in web worker
}
//from a web worker
if(isIE) {
//give the exact url so the xhr doesn't get a 302
callXHR('https://s3.amazon.com/...');
} else {
//this will follow the redirect to https://aws...
callXHR('https://example.com/document/1234/download');
}
I decided on Option 2 because I didn't want to give up the web worker.
The answers provided here were focused on CORS or redirects, but none actually addressed the specific problem I was having.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With