当标题已经设置时,为什么在通过AJAX下载文件时会获得“Access-Control-Allow-Origin”?

时间:2015-03-03 02:41:43

标签: javascript ajax http-headers rackspace-cloudfiles

我通过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();

当明确设置“Access-Control-Allow-Origin”时,为什么我会收到此错误?

1 个答案:

答案 0 :(得分:0)

好吧,似乎重新上传图片并再次设置标题可能已经解决了问题。

作为一个有用的旁注,使用Fog我上传如下图像:

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