使用 angular js 读取 Set-Cookie 头信息并维护用户会话

read Set-Cookie header information and maintain user session using angular js

我是 angular js 的新手。现在我有一个向服务器发送请求的应用程序。登录工作正常,服务器响应 Set-Cookie 字段中的会话信息等数据。问题是我既不能使用 angular js 读取这些信息,也不能执行任何其他需要登录的操作。我尝试过的解决方案包括: This 但它给出了以下错误:

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.

当我尝试使用 ngCookies 模块时,每当我尝试访问会话数据时,它也会 returns null 这很有意义,因为我无法访问第一名。我该如何解决这个问题?哦,也许值得一提的是服务器在不同的域上并使用 C#。 任何帮助将不胜感激。

下面是登录控制器:

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');
        });
    };

正如您提到的您的服务器在另一个域中,您必须在您的服务器中启用 CORS 请求。 您发出请求的服务器必须实施 CORS 以授予 JavaScript 从您的网站访问权限。您的 JavaScript 无法授予自己访问其他网站的权限。

https://enable-cors.org

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

首先你应该阅读 CORS。或者如您的错误中所述,如果您是从后端进行开发的人,您也可以允许请求的来源,那么您引用的 post 中的代码将起作用。 看看this.