Set-cookie in response not set for Angular2 post request

后端 未结 3 1463
栀梦
栀梦 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 (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

提交回复
热议问题