0

I can't seem to send $http.post requests for authentication. My code matches the solution on this page AngularJS withCredentials but I get this error through my browser console.

Credentials flag is 'true', but the 'Access-Control-Allow-Credentials' header is ''. It must be 'true' to allow credentials.

This is what the code looks like:

$scope.authenticate = function (user, pass) {
    $http.post(authUrl, {
        username: user,
        password: pass
    }, {
        withCredentials: true  
    })
}

I'm still new to angularJS but I read another site for help on setting custom headers, so I could fix the error message regarding the header. I tried to use:

$httpProvider.defaults.headers.post = { 'Access-Control-Allow-Credentials' : 'true' }

but I can't seem to get it right without $httpProvider undefined errors. I put it above the $http.post call, and also in the controller's function scope:

    .controller("authCtrl", function($scope, $http, authUrl, $httpProvider) {

Any ideas?

Community
  • 1
  • 1

2 Answers2

1

From Angular's documentation, you can only interact with providers during the configuration phase.

app.config(["$httpProvider", function ($httpProvider) {
    $httpProvider.defaults.withCredentials = true;
});
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • I get the same error. It seems this boolean is set to true but the header is not being updated. """Credentials flag is 'true', but the 'Access-Control-Allow-Credentials' header is "". It must be 'true' to allow credentials.""" –  Aug 15 '14 at 22:23
0

Access-Control-Allow-Credentials must be set on the server. Further reading:

https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Requests_with_credentials

tobib
  • 2,114
  • 4
  • 21
  • 35