Answer the question
In order to leave comments, you need to log in
Why cookies are not set on post request in Angular 5?
Good afternoon, I'm doing a project, the API is separate, the front part is separate in angular.
Everything is local.
I do a request from angular, and I get a response via api, but this cookie is not installed in the browser, in the cookie section in dev tools. Although it exists and is sent back. How can I get it into code?
here is a request from the front
const httpOptions = {
withCredentials: true,
headers: new HttpHeaders({
'Content-Type': 'application/json',
}),
};
const login = this.http.post('http://api/dashboard/login', body, httpOptions);
login.subscribe(
(response: HttpResponse) => {
this.doSomething(response);
}, (err) => {
console.log('Error: ' + err);
});
Header set X-Content-Type-Options "nosniff"
Header set X-XSS-Protection "1; mode=block"
Header always set Access-Control-Max-Age 1728000
Header always set Access-Control-Allow-Origin: "http://localhost:4200"
Header always set Access-Control-Allow-Methods: "GET,POST,OPTIONS,DELETE,PUT"
Header always set Access-Control-Allow-Headers: "Content-Type, Range, Origin, X-Requested-With,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control, Cache ,set-cookie,cookie, dashboard_token, token
Header always set Access-Control-Allow-Credentials true
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Content-Type, Range, Origin, X-Requested-With,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control, Cache ,set-cookie,cookie, dashboard_token, token
Access-Control-Allow-Methods: GET,POST,OPTIONS,DELETE,PUT
Access-Control-Allow-Origin: http://localhost:4200
Access-Control-Max-Age: 1728000
Connection: Keep-Alive
Content-Length: 121
Content-Type: application/json;charset=utf-8
Date: Fri, 08 Jun 2018 14:14:24 GMT
Keep-Alive: timeout=10, max=100
Server: Apache
Set-Cookie: dashboard_token=20338a95cb18498b17de9119792cd277; path=; expires=Fri, 15-Jun-2018 14:14:24 UTC
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Answer the question
In order to leave comments, you need to log in
The domains seem to be different, Angular sits on localhost:4200, and the apishka sits on api
In general, I myself recently learned about proxying Angular requests to other domains/ports - https://github.com/angular/angular-cli/blob/master..
I have this config (proxy.conf.json )
{
"/data/**": {
"target": "http://booking.localhost/",
"logLevel": "debug",
"changeOrigin": true
},
"/login": {
"target": "http://booking.localhost/",
"logLevel": "debug",
"changeOrigin": true
},
"/logout": {
"target": "http://booking.localhost/",
"logLevel": "debug",
"changeOrigin": true
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question