javascript - xhr 资源下载请求的 CORS 问题

标签 javascript jquery cors

xhr.open('GET', 'url/url_filePath=mydownload.zip', true);
    xhr.responseType = 'blob';
    let that = this;
    
    xhr.onload = function(e) {
      if (this.status == 200) {
        let data = this.response;
        const blob = new Blob([data], {type: 'application/octet-stream'});
        const file = new File([blob], 'mydownload.zip', {type: 'application/zip'});
        that.han
        that.setState((prevState, props) => {
          return {
            install : false
          };
        });
      }
    };
    xhr.send();

我有上面的代码行来下载和上传 zip 文件。每次我尝试发出下载请求时,都会收到“Access-Control-Allow-Origin” header 不存在错误。我用一个 chrome 扩展来解决这个问题,但我需要一个不需要 chrome 扩展的可扩展解决方案。有什么指点吗?

最佳答案

服务器发送的 CORS header 基本上说明了允许哪些源从该特定服务器请求资源。来自 MDN :

For security reasons, browsers restrict cross-origin HTTP requests initiated from within scripts. For example, XMLHttpRequest and Fetch follow the same-origin policy. So, a web application using XMLHttpRequest or Fetch could only make HTTP requests to its own domain.

要允许您的 Web 应用程序从该源获取资源,服务器必须发送 CORS header 。

格式应遵循:Access-Control-Allow-Origin:https://example.com

关于javascript - xhr 资源下载请求的 CORS 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46588041/

相关文章:

javascript - 遍历对象以找到两个 ID 变量的匹配项

javascript - mvc Javascript 文件在文件更改后不更新

jquery - 如何读取 jQuery 中默认的 ajax 参数?

javascript - SlickGrid 中可以使用可变行高吗?

javascript - Cordova 中的 CORS 重定向

cors - “No ' Access-Control-Allow-Origin ' header is present” 用于将请求重定向到 https ://login. microsoftonline.com/

cors - iPhone 模拟器中的 Ionic 3 CORS 问题

javascript - Rails 在旧形式而不是模态上显示错误

javascript - 在 Opera 中更改正文元素类

javascript - 根据它的值更改选定的单选按钮背景