CORS XMLHttpRequest在IE10-11 web worker中失败

bos*_*nou 8 javascript ajax internet-explorer amazon-s3 web-worker

我正试图XMLHttpRequest 从一个网络工作者中做出一个交叉起源.设置如下:

  • 原始请求发送到同一个域 example.com
  • 服务器将请求重定向(302)为 s3.amazon.com
  • 为CORS正确设置S3,使用正确的Access-Control-Allow-Origin标头进行响应

代码如下:

var xhr = new XMLHttpRequest();
//this will redirect to 'https://s3.amazon.com/...'
xhr.open('GET', 'https://example.com/document/1234/download');
xhr.send(null);
Run Code Online (Sandbox Code Playgroud)

Win 10上的Chrome,Firefox,Safari和MS Edge

从主JS文件和Web worker调用时,此代码都正确地遵循重定向.

Win 7上的IE 10/11

只有从主JS文件调用时,此代码才会正确地遵循重定向.从Web worker调用时,请求将中止而不会显示错误或日志消息.

可以通过编辑浏览器安全设置并启用"跨域访问数据源"来完成此工作,但期望用户这样做是不可行的.

问题

  1. 是否需要一些特殊设置才能使IE 10/11遵循重定向?
  2. 除了不透明的中止请求之外,有没有办法从IE 10/11获得更好的输出?

bos*_*nou 1

对于这个问题有几种可能的解决方法,我探索了其中的两种:

选项1

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
}
Run Code Online (Sandbox Code Playgroud)

选项2

//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');
}
Run Code Online (Sandbox Code Playgroud)

我决定选择选项 2,因为我不想放弃 Web Worker。

这里提供的答案主要集中在 CORS 或重定向上,但没有一个真正解决了我遇到的具体问题。