var module = angular.module('timestamp-marker-example', []);
module.factory('timestampMarker',[function() {
var timestampMarker = {
request: function(config) {
console.log(config);
console.log(config.method);
console.log(config.url);
/* console.log(config.headers);*/
var head={
headers:{
'X-testing':'testing'
},
method:"POST"
}
config.push(head);
return config ;
},
response: function(response) {
return response;
}
};
return timestampMarker;
}]);
module.config(['$httpProvider', function($httpProvider) {
$httpProvider.interceptors.push('timestampMarker');
}]);
module.controller('ExampleController', ['$scope', '$http', function($scope, $http) {
$http.get('https://api.github.com/users/naorye/repos').then(function(response) {
console.log(response);
});
}]);
我想从拦截器向请求添加新标头。如何添加?我试过上面的代码。任何人都可以帮我解决这个问题吗?
答案 0 :(得分:7)
配置:
app.config([ '$httpProvider', function($httpProvider) {
$httpProvider.interceptors.push('APIInterceptor');
} ]);
服务:
app.service('APIInterceptor', [function() {
var service = this;
service.request = function(config) {
config.headers.YOUR_HEADER= YOUR_HEADERS_VALUE;
return config;
};
}]);