2

I am new to angular js. Now I have an application that sends requests to the server. Login is working fine and the server responds with, among other data, the session information in the Set-Cookie field. The problem is I can neither read this information using angular js nor perform any other operations that require logging in. Among the solutions I have tried are; This but it gives the error below:

Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin http://localhost:52000 is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

When I try using the ngCookies module, that also returns null whenever I try to access session data which makes sense because I can not access that data in the first place. How can I solve this issue? Oh, perhaps its worth mentioning that the server is on a different domain and using C#. Any help will be highly appreciated.

Below is the login controller:

appAuth.controller('ctlLogin', function ($scope, $http, $cookies, $rootScope) {
    $scope.userCreds = {};
    var config = { withCredentials: true };
    $scope.login = function () {
        $http.post(urlLogin, $scope.user, config).then(function onSuccess(response) {
            console.log('success');
            var session = $cookies.get('Set-Cookie');
            console.log(session);

        }, function onFailure(response) {
            console.log('failure');
        });
    };
gordon
  • 191
  • 1
  • 2
  • 12

2 Answers2

1

First you should read about CORS. Or as mentioned in your Error, you can also allow the origin of the request if you are the one doing the development from the back end, then the code from the post you have referenced will work. Look at this.

guthik
  • 404
  • 1
  • 7
  • 15
0

As you mention your server is on another domain,you have to enable CORS request in your server. The server you are making the request to has to implement CORS to grant JavaScript from your website access. Your JavaScript can't grant itself permission to access another website.

https://enable-cors.org

https://developer.mozilla.org/en-US/docs/Web/HTTP/Server-Side_Access_Control