2013-03-14 56 views
2

我在使用Firefox和應用程序緩存做一些跨源請求時遇到問題。Firefox上的HTML5緩存清單阻止了Cross Origin請求

我XHR請求的錯誤處理程序被調用,以及XHR請求的狀態爲0

當我看到螢火蟲的網絡日誌中,我看到一個OPTIONS請求看起來不錯:

OPTIONS /foo.bar HTTP/1.1 
Host: localhost:1337 
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3 
Accept-Encoding: gzip, deflate 
Origin: http://localhost:8080 
Access-Control-Request-Method: GET 
Access-Control-Request-Headers: content-type 
Connection: keep-alive 

到的服務器響應的東西,看起來OK:

HTTP/1.1 200 OK 
Expires: Sun, 19 Nov 1978 05:00:00 GMT 
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 
Access-Control-Allow-Origin: http://localhost:8080 
Access-Control-Allow-Methods: GET, PUT, DELETE, OPTIONS 
Access-Control-Allow-Headers: content-type 
Date: Thu, 14 Mar 2013 17:55:22 GMT 
Connection: keep-alive 
Transfer-Encoding: chunked 

然後GET本身就沒有得到答覆:

GET /foo.bar HTTP/1.1 
Host: localhost:1337 
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3 
Accept-Encoding: gzip, deflate 
Origin: http://localhost:8080 
Connection: keep-alive 

(當在服務器日誌看,服務器永遠不會收到該請求)

我使用的是HTML5應用程序緩存機制,並在這裏是我的清單的網絡部分:

NETWORK: 
default.php 
resource.php 
http://localhost:1337/ 

這裏是我的嘗試:

  • 清單文件與*替換http://localhost:1337/:它的工作原理,但我不立當它涉及到檢測丟失的CACHE條目時,我發現阻止了非顯式網絡請求。
  • POST方法代替GET方法:它可以工作,但我不喜歡它,因爲它在語義上是錯誤的(我試圖獲取資源,而不是發佈數據)。
  • 與定製,但-語義正確READ方法更換GET方法:這是行不通的,但很好玩。

這是我的理解,我想要做的事屬於W3C規範中的Changes to the networking model的第3步,應該按原樣工作。

所以,這一切後,我的問題是:

  • 我在做什麼錯?
  • 這是一個Firefox的錯誤? (我忘了告訴,我的網站就像在Chrome和IE10(是的,IE10,如Microsoft Internet Explorer版本10)
  • 如果我不得不做一個怪癖,使其與Firefox,工作哪一個,我應該做一個魅力?難道還有比2級壞的我發現了一個更好的解決方案?

回答

1

雖然the spechttp://localhost:1337在緩存清單應該是足夠的NETWORK部分,它可能是值得嘗試的完整URL(http://localhost:1337/foo.bar)看如果有一個在Firefox的實現中的錯誤。

如果不這樣的伎倆和所有的都失敗了,我只想去把*在您的NETWORK部分,至少在找出造成問題的原因之前。通過代碼適用於您的用戶的價值代碼。此外,還有其他方法可以在清單中查找缺失的條目。

1

A List Apart提到了這個問題:Application Cache is a Douchebag。見第9節。

您必須收聽每個回覆,然後自行篩選回覆或錯誤。

$.ajax(url).always(function(response) { 
    // Exit if this request was deliberately aborted 
    if (response.statusText === 'abort') { return; } // Does this smell like an error? 
    if (response.responseText !== undefined) { 
    if (response.responseText && response.status < 400) { 
     // Not a real error, recover the content resp 
    } else { 
     // This is a proper error, deal with it 
     return; 
    } 
    } 
    // do something with 'response' 
});