neo4j – 请求选项“withCredentials” – Angular2

我正在使用连接到Angular2组件中的Neo4j的请求获得CORS问题:

Response to preflight request doesn't pass access control check. A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin 'http://localhost:8080' is therefore not allowed access. The credentials mode of an XMLHttpRequest is controlled by the withCredentials attribute

如何设置withCredentials:false使用request(Typescript)?我假设这将解决问题.但请求ts文件未在其CoreOptions对象中列出withCredentials. Neo4j-Typescript软件包也没有在其Typescript定义中包含此内容.

最佳答案 您可以通过扩展BrowserXhr类来完成此操作:

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

并使用扩展名覆盖BrowserXhr提供程序:

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