我使用以下代码来请求文件:
function getData(imageEndpoint) {
return fetch(imageEndpoint, {
mode: 'cors'
})
.then(response => {
console.log(response);
})
.then(data => {
if (!('caches' in window)) {
return caches.open(cacheName)
.then(cache => {
return cache.put(imageEndpoint, data);
});
}
return data;
})
.catch(e => {
console.log('Request issue, ', e);
});
}
以下错误消息中的哪些输出:
Failed to load http://localhost:7000/image.jpg: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
P.S服务器适用于:8000
当我添加cors header
时 return fetch(imageEndpoint, {
mode: 'cors',
headers: {
'Access-Control-Allow-Origin': '*'
}
})
引发以下错误:
http://localhost:7000/image.jpg 405 (Method Not Allowed)
index.html:1 Failed to load http://localhost:7000/image.jpg: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 405. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
您能否建议如何设置请求以便成功接收文件?
答案 0 :(得分:0)
您需要在OPTIONS响应和POST响应中使用标头Access-Control-Allow-Origin:URL Here
或Access-Control-Allow-Origin:*。您应该在POST响应中包含标题Access-Control-Allow-Credentials:true。
您的OPTIONS响应还应包括标题Access-Control-Allow-Headers:origin,content-type,accept以匹配请求的标头。