Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs Angular2替换$httpProvider.defaults.withCredentials_Angularjs_Angular - Fatal编程技术网

Angularjs Angular2替换$httpProvider.defaults.withCredentials

Angularjs Angular2替换$httpProvider.defaults.withCredentials,angularjs,angular,Angularjs,Angular,我的问题和问题完全一样,只是角度2 总之,当向另一个域发送HTTP请求时,即使正确设置了CORS头,也不会发送JSESSIONID cookie。 Angular 1.x解决方案是设置以下配置: .config(function ($routeProvider, $httpProvider) { $httpProvider.defaults.withCredentials = true; //rest of route code 然而,我找不到任何替代解决方案的角度2 有什么想

我的问题和问题完全一样,只是角度2

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

.config(function ($routeProvider, $httpProvider) {
    $httpProvider.defaults.withCredentials = true;
    //rest of route code
然而,我找不到任何替代解决方案的角度2


有什么想法吗?谢谢

当您使用来自“@angular/Http”的导入{Http,Response,Headers}的Http服务时,您可以使用凭据设置: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();
            });;           
}
post(参数:字符串,对象:任意):可观察{
let headers=新的headers();
headers.append(“内容类型”、“应用程序/json”);
返回this.http.post(this.apirl+params,JSON.stringify(obj),{headers:headers,withCredentials:true})
.map((响应:响应)=>{
返回response.json();
});;           
}

您需要扩展默认Http服务并将withCredentials设置为true

http.service.ts

import { Injectable } from '@angular/core';
import { Headers, Http, Request, RequestOptions, Response, XHRBackend } from '@angular/http';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class HttpService extends Http {
  constructor(backend: XHRBackend, options: RequestOptions) {
    super(backend, options);
  }

  request(url: string | Request, options?: any): Observable<Response> {
    if (typeof url === 'string') {
        if (!options) {
            options = {headers: new Headers()};
        }
        options.withCredentials = true;
    } else {
        url.withCredentials = true;
    }

    return super.request(url, options);
  }
}
在模块文件中,您需要替换http服务

import { HttpServiceFactory } from './http.service.factory';
import { Http, RequestOptions, XHRBackend } from '@angular/http';

...

providers: [
  {
    provide: Http,
    useFactory: HttpServiceFactory,
    deps: [XHRBackend, RequestOptions]
  }
]

已经有一个关于这个的链接,你可以跟踪它。好的,谢谢,我会找到它的!仅供参考,若要在未准备就绪时继续工作,我已使用禁用本地浏览器上的检查
import { HttpServiceFactory } from './http.service.factory';
import { Http, RequestOptions, XHRBackend } from '@angular/http';

...

providers: [
  {
    provide: Http,
    useFactory: HttpServiceFactory,
    deps: [XHRBackend, RequestOptions]
  }
]