bos*_*nou 8 javascript ajax internet-explorer amazon-s3 web-worker
我正试图XMLHttpRequest 从一个网络工作者中做出一个交叉起源.设置如下:
example.coms3.amazon.comAccess-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)
从主JS文件和Web worker调用时,此代码都正确地遵循重定向.
只有从主JS文件调用时,此代码才会正确地遵循重定向.从Web worker调用时,请求将中止而不会显示错误或日志消息.
可以通过编辑浏览器安全设置并启用"跨域访问数据源"来完成此工作,但期望用户这样做是不可行的.
对于这个问题有几种可能的解决方法,我探索了其中的两种:
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)
//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 或重定向上,但没有一个真正解决了我遇到的具体问题。
| 归档时间: |
|
| 查看次数: |
948 次 |
| 最近记录: |