我正在使用javascript通过fetch()
方法获取一些数据:
fetch(url, {
method: 'GET',
mode: 'cors',
cache: 'no-cache',
credentials: 'same-origin',
headers: myHeaders,
referrer: 'no-referrer',
}
但是它给了我以下错误:
错误:- CORS 策略已阻止从源"http://localhost:8080"获取"https://www.dropbox.com/s/v2pca6kq8nsqmso/abb5c48dae55560e4ae7d41af7bfdc50.jpg?raw=1"的访问:对预检请求的响应未通过访问控制检查:请求的资源上不存在"访问控制-允许源"标头。如果不透明响应满足您的需求,请将请求的模式设置为"no-cors",以便在禁用 CORS 的情况下获取资源。
我错过了什么?
www.dropbox.com 不允许CORS。 它不包括 CORS 标头。 这是有意的.
您可以使用域 dl.dropboxusercontent.com 的 CDN 链接,这样做了。
请注意,Dropbox 不支持此类用法,可能会也可能不会停止或更改,恕不另行通知。
fetch('https://dl.dropboxusercontent.com/s/v2pca6kq8nsqmso/abb5c48dae55560e4ae7d41af7bfdc50.jpg?raw=1', {
method: 'GET',
mode: 'cors',
cache: 'no-cache',
credentials: 'same-origin',
headers: {},
referrer: 'no-referrer',
}).then(x => x.blob().then(y => console.log(y)))