为什么我得到“;访问控制允许起源”;当已经设置了头时,通过AJAX下载文件

Why do I get "Access-Control-Allow-Origin" when downloading a file via AJAX when the header is already set?

本文关键字:设置 文件 下载 AJAX 通过 访问控制 许起源 为什么 起源      更新时间:2023-09-26

我有一个通过Rackspace Cloud Files托管在CDN上的映像。我将"Access Control Allow Origin"标题设置为"*":

curl -I http://ddfe38685b82abf2025a-3d3d68979b9884dbb99ee38fe4e87955.r49.cf1.rackcdn.com/images/ads/6/a6c3d7ec7ca8d8a41a4a065e27e317e2.jpg
HTTP/1.1 200 OK
Content-Length: 1304399
Accept-Ranges: bytes
Last-Modified: Tue, 03 Mar 2015 02:10:06 GMT
ETag: c8767efedb8057eaa637a2caeabed834
X-Timestamp: 1425348605.85405
Access-Control-Allow-Origin: *
Content-Type: image/jpeg
X-Trans-Id: tx5115bede82244690acd6a-0054f51e84dfw1
Cache-Control: public, max-age=259200
Expires: Fri, 06 Mar 2015 02:37:57 GMT
Date: Tue, 03 Mar 2015 02:37:57 GMT
Connection: keep-alive

然而,当我尝试通过AJAX下载这个文件时,我得到了No 'Access-Control-Allow-Origin' header is present on the requested resource:

var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://ddfe38685b82abf2025a-3d3d68979b9884dbb99ee38fe4e87955.r49.cf1.rackcdn.com/images/ads/6/a6c3d7ec7ca8d8a41a4a065e27e317e2.jpg', true);
xhr.send();

有什么想法吗?当"访问控制允许来源"明确设置时,我为什么会出现这个错误?

嗯,重新上传图像并重新设置标题似乎已经解决了问题。

作为一个有用的附带说明,使用Fog我上传图像如下:

directory.files.create({
    :key => image_file_path,
    :body => File.read(image_temp_path),
    :public => true,
    'Access-Control-Allow-Origin' => '*'
})