Angular2替换$ httpProvider.defaults.withCredentials

foc*_*och 5 angularjs angular

我有与问题完全相同的问题,但对于Angular 2.

总而言之,当向另一个域发送HTTP请求时,即使正确设置了CORS头,也不会发送JSESSIONID cookie.Angular 1.x解决方案是设置以下配置:

.config(function ($routeProvider, $httpProvider) {
    $httpProvider.defaults.withCredentials = true;
    //rest of route code
Run Code Online (Sandbox Code Playgroud)

但是我找不到Angular 2的任何替代解决方案.

任何的想法?谢谢!

小智 0

当您使用 Http 服务 from import { Http,Response, Headers } from '@angular/http' 时,您可以设置 withCredentials: true;

post(params:string, obj:any): Observable<any> {
    let headers = new Headers();
    headers.append("content-type", "application/json"); 
    return this.http.post(this.apiUrl + params, JSON.stringify(obj), { headers: headers, withCredentials: true })
        .map((response : Response)=>{
                return response.json();
            });;           
}
Run Code Online (Sandbox Code Playgroud)