[英]Control of headers in a $http request
I am struggle with header caching in a request. 我为请求中的标题缓存而苦苦挣扎。
my resource is looks like: 我的资源如下:
method: 'GET',
cache: false,
headers: {
session: auth.mySession()
}
Provider is also configured. 提供程序也已配置。
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';
}]).
In practice it doesn't work. 实际上,它不起作用。 Request is sent with a session while it is removed and without the session while it's set.
请求在删除时与会话一起发送,而在设置时不与会话一起发送。 I had to press Ctrl+F5.
我必须按Ctrl + F5。
At last I realized that it is not about caching results, but caching headers themselves. 最后,我意识到这不是缓存结果,而是缓存标题本身。
Headers are calculated just once at page initialization stage. 页眉在页面初始化阶段仅计算一次。 Right after that fact I came with an interceptor solution, which always injects valid session into a request's header.
之后,我提供了一个拦截器解决方案,该解决方案始终将有效会话注入请求的标头中。
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;
};
}]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.