【问题标题】:Cookies work great on localhost but does not work on VPSCookies 在本地主机上运行良好,但在 VPS 上不起作用
【发布时间】:2018-03-17 01:29:17
【问题描述】:

我正在使用 nodejs 和 angularjs 1.6.4。在 localhost 上一切正常,但是当我通过 Github 将代码推送到 Digital Ocean droplet 时,cookie 不起作用。我保存到 cookie 的所有内容都不会出现在 Cookie 存储中。为什么会这样?

更新 当我在 VPS 上进入登录页面时出现警告:"This page includes a password or credit card input in a non-secure context. A warning has been added to the URL bar"

下面是相关代码:

$scope.login = function () {

      UserService.login($scope.user).then(function (result) {
        if (result.data.success) {
          $rootScope.userLogin = result.data.data.name;

          //------------------
          var day = new Date();
          day.setDate(day.getDay() + 30);

          var options = {
            domain: "localhost",
            httpOnly: true,
            expires: day
          };
          // cookie does not work, nothing in Cookie Storage
          // but it works perfectly on localhost
          $cookies.put('token', result.data.data.token, options);
          $cookies.put('name', result.data.data.name, options);
          // the session storage work greatly
          $sessionStorage.user = 'heheeheh';
          flash.success = result.data.message;
          $state.go('home');
        } else {
          flash.error = result.data.message;
        }
      });
    } 

更新:

我在亚马逊 ec2 上创建了另一个 VPS,但它仍然无法正常工作。

【问题讨论】:

  • 控制台中是否显示任何错误或警告?
  • 另外,请贴出相关代码。

标签: angularjs node.js cookies vps


【解决方案1】:

我发现只需将 domain 选项从 localhost 更改为服务器的 IP

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-24
    • 1970-01-01
    • 1970-01-01
    • 2020-02-25
    • 2017-07-22
    • 2017-02-08
    • 1970-01-01
    • 2018-05-24
    相关资源
    最近更新 更多