角2和控制HTTP请求和响应

角2和控制HTTP请求和响应

本文介绍了角2和控制HTTP请求和响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在角2我试图控制HTTP请求/响应设置/发送请求并得到回应时,读一些头。

In Angular 2 I am trying to control http request/response to set/read some headers when sending request and getting response.

我只覆盖了Htt prequest这样的,它是为我工作:

I just override HttpRequest like this, and it is working for me :

@Injectable()
export class HttpRequest extends RequestOptions {
    constructor() {
         super({
             method: RequestMethod.Get,
             headers: new Headers({
                'X-Some-Header': 'some-content'})
        });
    }
}

但首要响应我有问题:

But for overriding Response I have problem :

@Injectable()
export class HttpResponse extends Response {
    constructor(responseOptions: ResponseOptions) {
        super(responseOptions);
        console.log("RESPONSE IS CREATED!!!!!!!");
    }
}

构造从来没有被调用,就是这样引导:

The constructor never being called and here it is the bootstrap:

bootstrap(AppComponent,
    [   ROUTER_PROVIDERS
        ,HTTP_PROVIDERS
        ,provide(RequestOptions, {useClass: HttpRequest})
        ,provide(Response, {useClass: HttpResponse})
    ]);

原因覆盖的反应是阅读一些响应头和404页不控制全球发现....

The reason for overriding the response is to read some response headers and control for 404-Not Page Found globally and ....

谢谢,

推荐答案

您可以在几个方面实现:创建基本服务类或提供自定义XHR实现:

You could implement it in a few ways: create base service class or provide custom xhr implementation:

@Injectable()
export class CustomBrowserXhr extends BrowserXhr {
  constructor() {}
  build(): any {
    let xhr:XMLHttpRequest = super.build();
    /*...add headers, listeners etc...*/
    return <any>(xhr);
  }
}

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

这篇关于角2和控制HTTP请求和响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 21:45