angular - 如何以角度返回整个 HTTP 响应以查看特定的响应标头?
问题描述
我正在尝试在我的 Angular 应用程序中检查特定的响应标头。如果用户尝试使用过期的 JWT 访问需要有效 JWT 的路由,则来自后端 ASP.NET Core 服务器的响应标头具有token-expired: true
. 我想在将用户发送到刷新路线之前检查该值。
我尝试了以下方法来查看整个响应标头,但没有成功:
`public fetchPosts(): Observable<HttpResponse<Object>> {
var b = this.http.get<HttpResponse<Object>>('api/v1/posts', {observe: 'response'}).pipe(
tap(resp => console.log('THE RESPONSE FOR FETCH POSTS IS:', resp))
)
console.log('THIS IS VALUE OF B: ', b);
return b;
}`
的值b
始终是这样的:
`
Observable {_isScalar: false, source: Observable, operator: DoOperator}
post.service.ts:20
_isScalar:false
operator:DoOperator {nextOrObserver: , error: undefined, complete: undefined}
complete:undefined
error:undefined
nextOrObserver:function (resp) { … }
[[FunctionLocation]]:internal#location
[[Scopes]]:Scopes[2]
arguments:TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
caller:TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
length:1
name:""
prototype:Object {constructor: }
`
我知道我需要在后端服务器上专门公开某些响应标头,但是b
对象具有未定义的error
和complete
键的事实让我想知道我是否真的做错了什么?
任何见解将不胜感激。
解决方案
Http 响应及其标头是当 HttpClient 从服务器接收到错误响应时作为错误事件发出的,如指南中所述。因此,您只需要在订阅(或使用tap()
or catchError()
)时提供一个错误处理程序。
.pipe(
catchError(error => {
const responseHeader = error.headers.get('<header_name>');
if (responseHeader) {
// send to /refresh-token route
}
})
)
推荐阅读
- php - 用 php 启动 selenium webdriver
- flash - 在 Flash CS6 中编辑具有嵌入字体的 SWF 文件
- python - 如何获得字符保持相同顺序的部分编码字符串的所有可能组合?
- javascript - 在 TypeScript 中实现装饰器模式
- cookies - Graphql Apollo 设置 cookie 不起作用
- reactjs - Material Popover 可滚动内容
- git - 为什么我不能在 IntelliJ 中删除本地 Git 分支?
- python - 你如何观察可调用对象的参数和返回值?
- css - Slick Slider 覆盖了下面的元素
- c - 在 C 中使用矩阵绘图