【发布时间】:2017-11-17 07:10:02
【问题描述】:
我有一个加载在 asp net core 中的 Angular 客户端应用程序。来自 asp net core 到 angular 应用程序的初始请求提供了一些标头,我如何以 angular 捕获这些标头以便以后重用它们。 我能够捕获标头或拦截由 angular 发出的调用,我如何从初始响应中捕获标头。请提出建议。
【问题讨论】:
标签: angular http header token httphandler
我有一个加载在 asp net core 中的 Angular 客户端应用程序。来自 asp net core 到 angular 应用程序的初始请求提供了一些标头,我如何以 angular 捕获这些标头以便以后重用它们。 我能够捕获标头或拦截由 angular 发出的调用,我如何从初始响应中捕获标头。请提出建议。
【问题讨论】:
标签: angular http header token httphandler
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 可能会有所帮助
【讨论】: