【问题标题】:Asp core -Angular 2 -Capturing Headers from initial rendering from serverAsp core -Angular 2 - 从服务器的初始渲染中捕获标头
【发布时间】:2017-11-17 07:10:02
【问题描述】:

我有一个加载在 asp net core 中的 Angular 客户端应用程序。来自 asp net core 到 angular 应用程序的初始请求提供了一些标头,我如何以 angular 捕获这些标头以便以后重用它们。 我能够捕获标头或拦截由 angular 发出的调用,我如何从初始响应中捕获标头。请提出建议。

【问题讨论】:

    标签: angular http header token httphandler


    【解决方案1】:

    GET 标头示例

    http
      .get<MyJsonData>('/data.json', {observe: 'response'})
      .subscribe(resp => {
        // Here, resp is of type HttpResponse<MyJsonData>.
        // You can inspect its headers:
        console.log(resp.headers.get('X-Custom-Header'));
        // And access the body directly, which is typed as MyJsonData as requested.
        console.log(resp.body.someField);
      });
    

    带有标题的 POST 示例

    http
      .post('/api/items/add', body, {
        headers: new HttpHeaders().set('Authorization', 'my-auth-token'),
      })
      .subscribe();
    

    HttpHeaders 类是不可变的,因此每个 set() 都会返回一个新实例并应用更改。

    更多详情https://angular.io/guide/http

    编辑:我不确定你以后使用它们是什么意思 - 如果刷新页面后需要它们,也许你可以将它们保存到 SessionStorage 或 LocalStorage。

    也检查一下:Accessing the web page's HTTP Headers in JavaScript 可能会有所帮助

    【讨论】:

      猜你喜欢
      • 2015-10-16
      • 2018-12-13
      • 1970-01-01
      • 1970-01-01
      • 2016-11-08
      • 2017-08-19
      • 1970-01-01
      • 1970-01-01
      • 2016-08-27
      相关资源
      最近更新 更多