2014-06-23 44 views
1

我想通過AJAX發送文件到rackspace。這是我第一次看CORS。我的文檔中看到發送預檢要求的選項,但是因爲我個人設置的頭,我知道自己的起源是有效的,我想放棄,這些都是從我的上傳端點標題:CORS請求立即取消鉻

HTTP/1.1 204 No Content 
Content-Length: 0 
X-Container-Object-Count: 2 
Accept-Ranges: bytes 
X-Container-Meta-Access-Log-Delivery: false 
X-Container-Meta-Access-Control-Expose-Headers: etag location x-timestamp x-trans-id 
X-Timestamp: 1401852621.29287 
X-Container-Meta-Access-Control-Allow-Origin: h ttp://localhost:8080**<-- (manually added the space after "h" so stackoverflow would let me submit) 
X-Container-Bytes-Used: 5572910 
Content-Type: text/plain; charset=utf-8 
X-Trans-Id: txfc64055cb1114b6fb0ef6-0053a77a46ord1 
Date: Mon, 23 Jun 2014 00:52:22 GMT 

但是,每當我嘗試發送鍍鉻它立即失敗,出現以下消息的請求:

XMLHttpRequest cannot load [**I'm redacting my actual endpoint**]. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'h ttp://localhost:8080' is therefore not allowed access. 

這裏是我的請求頭:

Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryZSg4nEq8EDaXQQBu 
Origin:h ttp://localhost:8080 
Referer:h ttp://localhost:8080/tools/artwork 
<-- (manually added the space after "h" so stackoverflow would let me submit) 
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36 

我是什麼錯誤唱?即使您知道原產地被允許,是否需要預檢請求?我從來沒有看到一個包回來,似乎Chrome不發送?

+0

http://stackoverflow.com/questions/10883211/deadly-cors-when-http-localhost-is-the-origin – epascarello

回答

-1

在您的服務器中,添加Access-Control-Allow-Origin: http://foo.example標題。

例如,在春季控制器,response.setHeader("Access-Control-Allow-Origin", "http:localhost:8080");

附加的東西,

Access-Control-Allow-Origin: http://foo.example // you can add as many urls separated by commas or '*' to allow all urs 
Access-Control-Allow-Methods: POST, GET, OPTIONS // Request method options separated by commas 
Access-Control-Allow-Headers: X-PINGOTHER 
Access-Control-Max-Age: 1728000  // expiration in milliseconds 

請參閱本網站MDN

+0

端點是rackspace,它出現在他們的文檔中(http:// docs。 rackspace.com/files/api/v1/cf-devguide/content/CORS_Container_Header-d1e1300.html)似乎不允許我設置「Access-Control-Allow-Origin」只是X-Container-Meta-Access-Control -Allow-Origin –

+0

按照[Documentation](http://docs.rackspace.com/files/api/v1/cf-devguide/content/CORS_Container_Header-d1e1300.html),他們只允許三個標題。那麼你把所有三個都設置好了嗎? – Bharath

2

是的,只要您的CORS請求不是「簡單」變種,就意味着您有除GET,HEAD或POST之外的其他方法,而不是application/x-www- form-urlencoded,multipart/form-data或text/plain,或者您的請求設置自定義標題。

但是,無論如何,您粘貼的回覆首先不包含Access-Control-Allow-Origin(它有X-Container-Meta-Access-Control-Allow-Origin),這就是您的請求被拒絕的原因。

+0

Jeff,這是我試圖發送的帖子,並且根據rackspace文檔,他們似乎不支持設置Access-Control-Allow-Origin標題,只是「X-Container-Meta-Access-Control-允許來源「標頭 –

+0

如果原始服務器不發回正確的標題,則無法執行任何操作。這就是CORS的工作原理。 –

+0

就是這樣!我的請求設置了自定義標題 –