Asp核心--Angular 2 -Capturing来自服务器初始渲染的标题

时间:2017-11-17 07:10:02

标签: angular http header token httphandler

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

1 个答案:

答案 0 :(得分:0)

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可能有帮助