我正在尋找一種優雅的方式來存儲與AngularJS的http請求。 我正在開發一個在Cordova/Phonegap上運行的移動應用程序,在這裏我們遇到問題,網絡可能會丟失。AngularJS:存儲失敗的http請求,然後再試
當用戶寫評論但無法將其發送到服務器時,請求應該被存儲(我認爲LocalStorage將是一個好地方),並在服務器再次可用時自動發送。
我有兩個問題: 1.是否有任何可以使用的解決方案? 2.如果不是,你會如何設計這樣的事情?
我已經有一個$ HTTP攔截,偵聽失敗state==0
請求,並在請求方{retry: false}
設置一個標誌:
responseError: function(response) {
// something went wrong, report to airbrake
if(response.status==0 && response.config.retry) {
// don't reject the response, user should see everything is ok
// somehow here we should store the request, and try it later
return response;
}
return $q.reject(response);
}
我會用那麼存儲在localStorage的,並試圖請求服務他們以後。但我不確定,如果這是正確的路,或者我想複雜。
這似乎是正確的做法。將請求配置存儲在localStorage中,並在應用程序加載時逐個發送請求。 – SoluableNonagon 2014-11-14 18:59:01
你可以使用https://github.com/grevory/angular-local-storage這個存儲。 – SoluableNonagon 2014-11-14 19:00:48