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/