当我在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
Run Code Online (Sandbox Code Playgroud)
后端在Django 1.8中编程.
有没有人经历过同样的事情或有建议如何解决这个问题?
max*_*lec 20
确实是一个CORS问题.从Angular2 RC2开始,你只需要
this.http.get('http://my.domain.com/request', { withCredentials: true })
Run Code Online (Sandbox Code Playgroud)
Thi*_*ier 12
我似乎是一个与CORS相关的问题.也许您可以尝试withCredentials在执行HTTP请求时设置属性.
这个答案可以帮助你找到如何做到这一点,特别是Cedric Exbrayat的回答:
编辑
你可以扩展BrowserXhr:
@Injectable()
export class CustomBrowserXhr extends BrowserXhr {
constructor() {}
build(): any {
let xhr = super.build();
xhr.withCredentials = true;
return <any>(xhr);
}
}
Run Code Online (Sandbox Code Playgroud)
并BrowserXhr使用扩展覆盖提供者:
bootstrap(AppComponent, [
HTTP_PROVIDERS,
provide(BrowserXhr, { useClass: CustomBrowserXhr })
]);
Run Code Online (Sandbox Code Playgroud)
如果您需要有关CORS的更多提示,可以查看以下链接:http://restlet.com/blog/2015/12/15/understanding-and-using-cors/.
希望它对你有帮助,蒂埃里
| 归档时间: |
|
| 查看次数: |
18105 次 |
| 最近记录: |