简体   繁体   English

控制$ http请求中的标头

[英]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.

相关问题 在Access-Control-Request-Headers下添加$ http标头 - $http headers gets added under Access-Control-Request-Headers 具有$ http的Access-Control-Allow-Header不允许使用请求标头字段 - Request header field is not allowed by Access-Control-Allow-Headers with $http 自定义HTTP标头总是添加在“Access-Control-Request-Headers”下 - Custom HTTP headers always get added under “Access-Control-Request-Headers” $ http.post-请求标头字段Access-Control-Allow-Headers不允许授权 - $http.post - Request header field Authorization is not allowed by Access-Control-Allow-Headers AngularJS:缺少标头的http请求 - AngularJS: http request with missing headers $ http.post请求标头字段Access-Control-Allow-Headers错误不允许使用Access-Control-Allow-Origin - $http.post Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers error angularJS $ http.Post无法正常工作:无法加载资源:Access-Control-Allow-Headers不允许请求标头字段0 - angularJS $http.Post not working: Failed to load resource: Request header field 0 is not allowed by Access-Control-Allow-Headers 自定义标头已添加到Access-Control-Request-Headers中 - Custom headers get added to Access-Control-Request-Headers 在Angular2中发送带有标头的HTTP请求 - Sending an HTTP request with headers in Angular2 在矩形中为单个请求设置标头和http参数 - Setting headers and http params for a single request in restangular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM