i have a question regarding CORS requests with HTTP Authorization header:
It seems to me that web browser is not sending Authorization header with POST request, is there any way around this?
Here is my Angular code:
var app = angular.module('app', [])
.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
}]);
app.controller('ctrl', function ($scope, $http) {
$scope.insert = function () {
$http.post('http://my.api.com/Insert',
{
headers: {
'Authorization': 'Basic dGVzdDp0ZXN0',
'Content-Type': 'application/x-www-form-urlencoded'
},
data: {
'Code': 'test data'
},
withCredentials: true
});
};
});
On server side i have this in my web.config
<httpProtocol >
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With" />
<add name="Access-Control-Allow-Methods" value="GET,POST,PUT,DELETE,OPTIONS" />
<add name="Access-Control-Allow-Credentials" value="true" />
</customHeaders>
</httpProtocol>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…