简体   繁体   English

Angular 5 - 尽管(Set-Cookie)在响应标头中,但已设置 Cookie

[英]Angular 5 - Cookie is set although (Set-Cookie) is in the response header

For an Angular 5 app, I have an auth service that does a HTTP POST which returns the session cookie (CORS) as shown below in the code below:对于 Angular 5 应用程序,我有一个执行 HTTP POST 的身份验证服务,它返回会话 cookie (CORS),如下面的代码所示:

signIn(signInRequest: SignInRequest): Observable<SignInResponse> {
   let headers: Headers = new Headers();   
   headers.append('Content-Type','application/json');  
   return this.http
              .post("/login", {email:  signInRequest._email,password:signInRequest._password}, { headers: headers, withCredentials: true })
  .map(this.extractData)
  .catch(this.handleErrorObservable);}

The response of the header contains the set-cookie as shown below:标头的响应包含 set-cookie,如下所示:

在此处输入图像描述

and the request header is the following:请求标头如下:

在此处输入图像描述

I know that the browser should be setting the cookie response.我知道浏览器应该设置 cookie 响应。 Why is it not doing it?为什么不这样做?

Your frontend is hosted on localhost:4200 and your backend is hosted on api.safra.me .您的前端托管在localhost:4200上,后端托管在api.safra.me上。 By default, your browser won't send the cookies along the request unless you use the withCredentials in the login request as you already did, and all of the subsequent requests .默认情况下,您的浏览器不会随请求发送 cookie,除非您像之前那样在登录请求和所有后续请求中使用withCredentials

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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