简体   繁体   中英

withCredentials AngularJS request POST error

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?

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

app.config(["$httpProvider", function ($httpProvider) {
    $httpProvider.defaults.withCredentials = true;
});

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM