向AngularJS应用程序添加自定义标题

向AngularJS应用程序添加自定义标题,angularjs,http-headers,custom-headers,Angularjs,Http Headers,Custom Headers,我想知道是否有一种方法可以将自定义标题添加到我的AngularJS应用程序中,而不必将它们添加到每个调用中 到目前为止,我的电话中的一个示例如下所示: $http.get('http://api.discogs.com/artists/' + $scope.artist.id + '/releases?page=' + $scope.nextPage + '&per_page=8').then(function(data2) { $scope.nextPage = $

我想知道是否有一种方法可以将自定义标题添加到我的AngularJS应用程序中,而不必将它们添加到每个调用中

到目前为止,我的电话中的一个示例如下所示:

$http.get('http://api.discogs.com/artists/' + $scope.artist.id + '/releases?page=' +       $scope.nextPage + '&per_page=8').then(function(data2) {
    $scope.nextPage = $scope.nextPage + 1;
    if($scope.nextPage > data2.data.pagination.pages){
       $scope.morePages = false;
    }
    $scope.releases = $scope.releases.concat(data2.data.releases);
})
.finally(function(){
    $scope.loading = false;
}); 
myService.makeCall(artistId, other parameters such as page no,.....,function(data2){
$scope.nextPage = $scope.nextPage + 1;
    if($scope.nextPage > data2.data.pagination.pages){
       $scope.morePages = false;
    }
    $scope.releases = $scope.releases.concat(data2.data.releases);

})
我在另一个问题中观察到,有人会像这样包含标题:

$http({method: 'GET', url: 'http://localhost:3000/api/symbol/junk', 
       headers:{
         'Access-Control-Allow-Origin': '*',
         'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
         'Access-Control-Allow-Headers': 'Content-Type, X-Requested-With',
         'X-Random-Shit':'123123123'
       }})
      .success(function(d){ console.log( "yay" ); })
但首先,我不知道如何将它集成到我的呼叫中,其次我假设这只适用于一个呼叫,而不是所有呼叫

有什么建议吗


谢谢

用你的方法打电话:

var makeCall = function(artistId,etc...,callBackFunc){
 var config={method: 'GET',
          url: 'http://api.discogs.com/artists/', 
          params:{artistId:artistId,etc....}
          headers:{
         'Access-Control-Allow-Origin': '*',
         'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
         'Access-Control-Allow-Headers': 'Content-Type, X-Requested-With',
         'X-Random-Shit':'123123123'
 }};
 $http(config).then(function(data){
    callBackFunc(data);
 })
}
并在控制器中使用它,如下所示:

$http.get('http://api.discogs.com/artists/' + $scope.artist.id + '/releases?page=' +       $scope.nextPage + '&per_page=8').then(function(data2) {
    $scope.nextPage = $scope.nextPage + 1;
    if($scope.nextPage > data2.data.pagination.pages){
       $scope.morePages = false;
    }
    $scope.releases = $scope.releases.concat(data2.data.releases);
})
.finally(function(){
    $scope.loading = false;
}); 
myService.makeCall(artistId, other parameters such as page no,.....,function(data2){
$scope.nextPage = $scope.nextPage + 1;
    if($scope.nextPage > data2.data.pagination.pages){
       $scope.morePages = false;
    }
    $scope.releases = $scope.releases.concat(data2.data.releases);

})
是的,这适用于一个请求,您可以使用
$http
拦截器将其应用于所有请求