2012-06-04 23 views
11

我有一個使用緩存清單提供離線功能的HTML5應用程序。此應用程序在線時會發出ajax呼叫,並且其中一些呼叫可能獲得403未經授權的響應。HTML5緩存清單:ajax調用不成功後退

這是我的cache.manifest文件的底部:

NETWORK: 
* 

FALLBACK: 
//offline 

如果我刪除回退段,所有接收到403響應工作Ajax調用如預期,我可以用jQuery的錯誤處理程序檢測到並重定向用戶登錄表單。

但是如果存在回退段,那麼即使服務器回答了403,同樣的調用也會收到200 OK響應,並且回退HTML的內容爲正文,因此我無法知道用戶未通過身份驗證,必須發送到登錄頁面。

我在這裏錯過了什麼嗎?在此先感謝

+0

你的意思是即使用戶在線並且文件真的不存在,fallback也會執行。 – Ekim

+0

是的,您期望在線通配符標誌('*')覆蓋您在回退中輸入的內容。但是按照[規範](http://www.w3.org/TR/2011/WD-html5-20110525/offline.html#parsing-cache-manifests) –

回答

2

將一個隨機數作爲查詢參數添加到您正在尋找jQuery-AJAX的頁面將解決該問題;即

$.ajax({ 
    url: "/data.html?"+ Math.random(), 
    // other properties here... 
}); 
0

http://alistapart.com/article/application-cache-is-a-douchebag#latest

這裏是一個錯誤可能是因爲狀態代碼的報告爲0,這被解釋爲一個失敗的發生參考:

$.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' 
});