【问题标题】:CORS XMLHttpRequest fails in IE10-11 web workerCORS XMLHttpRequest 在 IE10-11 web worker 中失败
【发布时间】:2017-04-13 23:21:14
【问题描述】:

我正在尝试创建一个跨域XMLHttpRequest 来自网络工作者。设置如下:

  • 原始请求是针对同一个域example.com
  • 服务器将请求重定向(302)到s3.amazon.com
  • S3 已为 CORS 正确设置,以正确的 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);

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

当从主 JS 文件和网络工作者调用时,此代码正确地遵循重定向。

Win 7 上的 IE 10/11

当从主 JS 文件调用时,此代码正确地遵循重定向。当从 Web Worker 调用时,请求被中止,没有错误或日志消息。

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

问题

  1. 是否需要一些特殊设置才能使 IE 10/11 跟随重定向?
  2. 除了不透明的中止请求之外,还有其他方法可以从 IE 10/11 获得更好的输出吗?

【问题讨论】:

  • 在阅读here 的评论部分后,我发现:“看起来您的问题是试图从不太安全的区域向更安全的区域发出 XHR 请求。”也许您的代码很好,最终代码会使用 https 托管吗?您是否有能力使用 https 测试您的代码而不将其推送到生产环境中?
  • 这一切都在 https 上进行了测试。我将更深入地查看这些 cmets,但乍一看,它们似乎并没有解决我的问题。感谢您的链接。
  • 您不能自己从响应头中读取重定向头并采取相应措施或 IE 10/11 吗?例如。在您的服务器上使用功能检测,然后如果浏览器不遵循重定向,请使用您的自定义 ajax 调用代码。我可以写一个例子,但只需要几天时间,因为我周末不使用笔记本电脑。
  • @praszyk 有多种解决方法,但显然我不想使用它们。我试图了解为什么这个特定案例会失败,以及它是否是 IE 中的错误。

标签: javascript ajax internet-explorer amazon-s3 web-worker


【解决方案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
}

选项 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');
}

我决定选择选项 2,因为我不想放弃 web worker。

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

【讨论】:

  • 您发送两个不同的网址。您在 IE 中的 302 重定向问题 this
【解决方案2】:

尝试在标头请求中设置更多元标记,例如 xhr.setRequestHeader('Access-Control-Allow-Origin', example.com); // 从哪个请求发出的

// Create the XHR object.
function createCORSRequest(method, url) {
  var xhr = new XMLHttpRequest();
  if ("withCredentials" in xhr) {
    // XHR for Chrome/Firefox/Opera/Safari.
    xhr.open(method, url, true);
  } else if (typeof XDomainRequest != "undefined") {
    // XDomainRequest for IE.
    xhr = new XDomainRequest();
    xhr.open(method, url);
  } else {
    // CORS not supported.
    xhr = null;
  }
  return xhr;
}

// Helper method to parse the title tag from the response.
function getTitle(text) {
  return text.match('<title>(.*)?</title>')[1];
}

// Make the actual CORS request.
function makeCorsRequest() {
  // This is a sample server that supports CORS.
  var url = 'https://example.com/document/1234/download';

  var xhr = createCORSRequest('GET', url);
  if (!xhr) {
    alert('CORS not supported');
    return;
  }

  // Response handlers.
  xhr.onload = function() {
    var text = xhr.responseText;
    var title = getTitle(text);
    alert('Response from CORS request to ' + url + ': ' + title);
  };

  xhr.onerror = function() {
    alert('Woops, there was an error making the request.');
  };

  xhr.send();
}

欲了解更多详情,请查看此链接 - https://www.html5rocks.com/en/tutorials/cors/

【讨论】:

  • 是的,但是在 IE 中如果你禁用了“跨域访问数据源”,它将无法工作
  • 我已经让 CORS 工作了。该问题是特定于来自网络工作者的重定向
  • 希望您可能已经尝试过...尝试从开发人员控制台调试 Web Worker [保留调试器]。由于不存在错误日志。我要求你做!并检查请求标头部分。尝试找出失败的原因。
猜你喜欢
  • 1970-01-01
  • 2018-03-06
  • 2019-03-21
  • 2016-09-21
  • 2012-06-26
  • 2012-11-30
  • 2018-03-10
  • 1970-01-01
  • 2014-06-02
相关资源
最近更新 更多