我正在开发一个AngularJS原型,用于为应用程序中的所有API请求注入自定义标头。我正在向外部API发出get请求。
当我通过DHC客户端添加自定义标头请求时,我收到了响应。如果我尝试通过Angular我会收到错误。
var portal = angular.module('portal',[]);
portal.controller('MainController', function($scope, $http) {
$scope.pageTitle = "All Country Names";
$http.get("https://restcountries.eu/rest/v2/all").then(function (response) {
// $http.get("welcome.html").then(function (response) {
$scope.responseData = response.data;
console.log(response.data);
});
});
portal.factory('httpRequestInterceptor', function () {
return {
request: function (config) {
config.headers['ServerSession'] = 'dfsdasdfasdfasdf';
config.headers['Cookie'] = 'asdfasdfa';
return config;
}
};
});
portal.config(function ($httpProvider) {
$httpProvider.defaults.headers.common['Access-Control-Allow-Headers'] = '*';
$httpProvider.interceptors.push('httpRequestInterceptor');
});
ServerSession和Cookie将从服务中检索并附加到标头中。
我引用了链接Request header field is not allowed by Access-Control-Allow-Headers with $http