【问题标题】:Getting cookies from HTTP response cookies to Angular从 HTTP 响应 cookie 获取 Angular 的 cookie
【发布时间】:2020-09-10 18:36:26
【问题描述】:

我试图让服务器响应 cookie 到 angular

component.ts

    this.api.doLogin(*mycredentials*).subscribe( res => {
    console.log(res);
}) 

service.ts

doLogin(*mycredentials*){
 return this.http.post(*myrequesturl* , {*mycredentials*});
} 

它像这样访问浏览器

如何在我的 console.log() 上获取这些信息?

【问题讨论】:

    标签: angular http cookies


    【解决方案1】:

    如果您的 cookie 是 httponly,那么出于安全原因您将无法访问它,否则您可以通过 document.cookie 访问它;

    或者你可以使用下面的函数。

    **

    function accessCookie(cookieName)
            {
              var name = cookieName + "=";
              var allCookieArray = document.cookie.split(';');
              for(var i=0; i<allCookieArray.length; i++)
              {
                var temp = allCookieArray[i].trim();
                if (temp.indexOf(name)==0)
                return temp.substring(name.length,temp.length);
              }
                return "";
            }
    

    **

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-14
      • 1970-01-01
      • 2014-02-26
      • 2017-01-04
      • 1970-01-01
      • 2012-10-02
      相关资源
      最近更新 更多