Set-cookie in response not set for Angular2 post request

后端 未结 3 1456
栀梦
栀梦 2020-12-05 16:04

When I make a put request in Angular2, I receive the expected set-cookie in the response. However my browser (tried both Chrome and Firefox) refuses to set the cookie.

相关标签:
3条回答
  • 2020-12-05 16:23

    I seems to be a CORS-related issue. Perhaps you could try to set the withCredentials attribute when executing the HTTP request.

    This answer could help you to find out how to do that, especially the Cedric Exbrayat's answer:

    • angular2 xhrfields withcredentials true

    Edit

    You could extend the BrowserXhr:

    @Injectable()
    export class CustomBrowserXhr extends BrowserXhr {
      constructor() {}
      build(): any {
        let xhr = super.build();
        xhr.withCredentials = true;
        return <any>(xhr);
      }
    }
    

    and override the BrowserXhr provider with the extended:

    bootstrap(AppComponent, [
      HTTP_PROVIDERS,
      provide(BrowserXhr, { useClass: CustomBrowserXhr })
    ]);
    

    If you need more hints about CORS, you could have a look at this link: http://restlet.com/blog/2015/12/15/understanding-and-using-cors/.

    Hope it helps you, Thierry

    0 讨论(0)
  • 2020-12-05 16:28

    Indeed a CORS issue. From Angular2 RC2 on, you just need to

    this.http.get('http://my.domain.com/request', { withCredentials: true })
    
    0 讨论(0)
  • 2020-12-05 16:30

    I had the same problem but for me the cookie had a path to '/api/order'.. So only request to this path contained the cookie.. I altered the path to '/' and now everthig is fine..

    0 讨论(0)
提交回复
热议问题