在 angular dart 上设置全局 Http 请求标头 [英] Setup global Http request Headers on angular dart

查看:31
本文介绍了在 angular dart 上设置全局 Http 请求标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何配置 Http 服务向调用添加标头.

How can I configure the Http service adding headers to the call.

我尝试以下

class GlobalHttpHeaders {
  static setup(Injector inj){
    HttpDefaultHeaders http = inj.get(HttpDefaultHeaders);
    http.setHeaders([{"X-Requested-With":"XMLHttpRequest"}], "COMMON");
  }
}

在应用程序中的最后一行是:

And in the app the last line is:

Injector inj = ngBootstrap(module: new SiteIceiModule());
  GlobalHttpHeaders.setup(inj);

但这不起作用.

推荐答案

(我认为)我得到了它:

(I think) I got it working with:

@Injectable()
class MyDefaultHeaders extends HttpDefaultHeaders {
  @override
  setHeaders(Map<String, String> headers, String method) {
    super.setHeaders(headers, method);
    //if(method.toUpperCase() == 'POST') {
    headers['X-Requested-With'] = 'XMLHttpRequest';
    //}
  }
}


@NgComponent(selector: 'my-comp', publishAs: 'ctrl', template:
    '<div>My component</div>')
class MyComponent {
  Http http;
  MyComponent(this.http) {
    //http.request('http://www.google.com/');
    var h = http;
        // debugger didn't show http so I filed https://code.google.com/p/dart/issues/detail?id=17486

    Map m = {};
    http.defaults.headers.setHeaders(m, 'GET');
    print(m);
    // prints:
    // {Accept: application/json, text/plain, */*, X-Requested-With: XMLHttpRequest}

  }
}


class MyAppModule extends Module {
  MyAppModule() {

    type(MyComponent);

    type(HttpDefaultHeaders, implementedBy: MyDefaultHeaders);

    init.createParser(this);
  }
}

我无法检查 http 以验证标头,因为调试器没有向我显示该字段,但如评论中所述当我应用 headers.setHeadersMyComponent 中做一个映射时,我得到了我的自定义标头(这就是 Httpheaders)我使用了 DI 0.0.33Angular 0.9.9

I couldn't examine http to verify the headers because the debugger didn't show me the field but as stated in the comment when I apply headers.setHeaders do a map inside MyComponent I get my custom header (this is what Http does with headers) I used DI 0.0.33, Angular 0.9.9

这篇关于在 angular dart 上设置全局 Http 请求标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆