gpt4 book ai didi

Angular 2 - 在每个 http 请求中附加 withCredentials = true

转载 作者:太空狗 更新时间:2023-10-29 18:26:26 24 4
gpt4 key购买 nike

我正在使用 angular 2(不是最新的,因为通过 angular-cli 使用:1.0.0-beta.11-webpack.9-4)并且我必须为每个 http 请求将 withCredentials 设置为 true。我尝试使用

为一个请求设置它

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

一切正常,但我正在尝试在 Bootstrap 中使用一些东西,如下所示,但没有取得任何成功

import './polyfills.ts';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { environment } from './environments/environment';
import { AppModule } from './app/';
import {Http, Headers, BaseRequestOptions, RequestOptions, BrowserXhr} from '@angular/http';

if (environment.production) {
enableProdMode();
}

export class MyRequestOptions extends BaseRequestOptions {
constructor () {
super();
this.headers.append('withCredentials','true');
}
}

platformBrowserDynamic().bootstrapModule(AppModule,
[{ provide: RequestOptions, useClass: MyRequestOptions}]
);

最佳答案

我不知道是否需要它了,但我遇到了同样的问题,我已经通过这种方式解决了:

  1. 创建 BaseRequestOptions 的子类(扩展 RequestOptions):

    import { Headers, BaseRequestOptions } from "@angular/http";

    export class AuthRequestOptions extends BaseRequestOptions {
    constructor() {
    super();
    this.withCredentials = true;
    }
    }
  2. 在应用程序 Bootstrap 中注册它

    import { RequestOptions } from '@angular/http';
    import { AuthRequestOptions } from './<path>/AuthRequestOptions';

    @NgModule({
    bootstrap: [...],
    imports: [...],
    providers: [
    { provide: RequestOptions, useClass: AuthRequestOptions},
    ...
    ]
    }...

(在我的例子中,这是与 CORS+NTLMAuthentication 一起工作的)

关于Angular 2 - 在每个 http 请求中附加 withCredentials = true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39743624/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com