Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
32
rated 0 times [  39] [ 7]  / answers: 1 / hits: 35622  / 11 Years ago, sun, october 13, 2013, 12:00:00

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>

More From » asp.net

 Answers
104

You're using the $http.post incorrectly. The second parameter is the data you need to send to server, you cannot set headers like this. In your case, it will send the whole object
as JSON payload



Try this:



$http({
url:'http://my.api.com/Insert',
method:POST,
headers: {
'Authorization': 'Basic dGVzdDp0ZXN0',
'Content-Type': 'application/x-www-form-urlencoded'
},
data: {
'Code': 'test data'
}
});

[#75024] Friday, October 11, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kieraelsies

Total Points: 718
Total Questions: 103
Total Answers: 104

Location: England
Member since Sun, May 21, 2023
1 Year ago
kieraelsies questions
Tue, Aug 3, 21, 00:00, 3 Years ago
Tue, Feb 23, 21, 00:00, 3 Years ago
Thu, Nov 12, 20, 00:00, 4 Years ago
Wed, Sep 9, 20, 00:00, 4 Years ago
Mon, Sep 16, 19, 00:00, 5 Years ago
;