記一個(gè)問題:
今天從阿里云的OSS服務(wù)里用XMLHttpRequest請(qǐng)求圖片(OSS已經(jīng)配置CORS),結(jié)果卻還是報(bào)CORS錯(cuò)誤
然后Chrome啟用Disable Cache 或請(qǐng)求頭里添加"Cache-Control","no-cache"后卻沒有問題了。
排查原因,發(fā)現(xiàn)是CORS安全響應(yīng)頭的問題,沒有匹配上OSS的CORS配置.
CORS 安全響應(yīng)頭列表和文檔:https://developer.mozilla.org/en-US/docs/Glossary/CORS-safelisted_response_header
XMLHttpRequest CORS 擴(kuò)展例子:
function createCORSRequest(method, url) {
let 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;
}
let req = new createCORSRequest('GET', 'https://xunchayun.oss-cn-beijing.aliyuncs.com/3dImage/20190715/9058134a-d375-4f36-8192-906c02576848.jpg');
xhr.setRequestHeader("Cache-Control","no-cache");
req.send();
本文轉(zhuǎn)載自https://www.cnblogs.com/linqing/p/11195521.html 林清