2017-07-19 125 views
0

我很擔心請求中的頭文件緩存。

我的資源是看起來像:

method: 'GET', 
cache: false, 
headers: { 
    session: auth.mySession() 
} 

提供商也配置。

config(['$httpProvider', function($httpProvider) { 
if (!$httpProvider.defaults.headers.get) { 
    $httpProvider.defaults.headers.get = {}; 
} 
$httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache'; 
$httpProvider.defaults.headers.get['Pragma'] = 'no-cache'; 
}]). 

在實踐中它不起作用。 請求在會話被移除的同時被髮送,而 在沒有會話的情況下被設置。 我不得不按下Ctrl + F5。

回答

0

最後我意識到這不是緩存結果,而是自己緩存頭部。

在頁初始化階段只計算一次頭。 在此之後,我提供了一個攔截器解決方案,它總是將有效會話注入請求的頭部。

config(['$httpProvider', function($httpProvider) { 
    $httpProvider.interceptors.push('injectingSessionInterceptor'); 
}]). 
service('injectingSessionInterceptor', ['auth', function (auth) { 
    var ser = this; 
    ser.request = function (config) { 
     var session = auth.mySession(); 
     config.headers.session = session; 
     return config; 
    }; 
}]);