簡體   English   中英

沒有為Angular2發布請求設置響應中的Set-cookie

[英]Set-cookie in response not set for Angular2 post request

當我在Angular2中發出put請求時,我在響應中收到了預期的set-cookie。 但是我的瀏覽器(嘗試過Chrome和Firefox)都拒絕設置cookie。

相反,當我使用Angular 1應用程序調用同一 API端點時,cookie被正確設置。

響應標頭是:

Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:http://example.com
Allow:GET, PUT, HEAD, OPTIONS
Content-Type:application/json
Date:Thu, 28 Jan 2016 14:41:38 GMT
P3P:policyref="http://www.example.com/p3p.xml", CP="NON DSP COR CURa TIA"
Server:WSGIServer/0.1 Python/2.7.6
Set-Cookie:sessionid=994wl49qfsizog5bqmt57sgx9q2toa25; expires=Mon, 28-Mar-2016 14:41:37 GMT; Max-Age=5183999; Path=/
Set-Cookie:csrf=u7UQhpAphTsGYKRU6jFlLFt6NoYAhNMS; Domain=api.example.com; expires=Thu, 26-Jan-2017 14:41:38 GMT; Max-Age=31449600; Path=/
Vary:Accept, Cookie

后端在Django 1.8中編程。

有沒有人經歷過同樣的事情或有建議如何解決這個問題?

確實是一個CORS問題。 從Angular2 RC2開始,你只需要

this.http.get('http://my.domain.com/request', { withCredentials: true })

我似乎是一個與CORS相關的問題。 也許您可以在執行HTTP請求時嘗試設置withCredentials屬性。

這個答案可以幫助你找到如何做到這一點,特別是Cedric Exbrayat的回答:

編輯

您可以擴展BrowserXhr

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

並使用擴展名覆蓋BrowserXhr提供程序:

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

如果您需要有關CORS的更多提示,可以查看以下鏈接: http//restlet.com/blog/2015/12/15/understanding-and-using-cors/

希望它對你有幫助,蒂埃里

我有同樣的問題,但對我來說,cookie有一個'/ api / order'的路徑..所以只有請求到這個路徑包含cookie ..我改變路徑為'/',現在everthig很好..

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM