我正在尝试在我的角度应用程序中检查特定的响应标头。如果用户尝试访问需要有效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
键,让我想知道我是否确实在做错事?
任何见识将不胜感激。
答案 0 :(得分:1)
如the guide中所述,当HttpClient从服务器接收到错误响应时,带有标题的Http响应就是作为错误事件发出的。因此,您只需在订阅(或使用public class Item
{
public int Id { get; set; }
public int? ParentId { get; set; }
public override int GetHashCode()
{
return Id.GetHashCode();
}
public override bool Equals(object obj)
{
return Id == (obj as Item)?.Id;
}
}
或public class Program
{
private static void Main(string[] args)
{
var list1 = new List<Item>
{
new Item {Id = 1, ParentId = null},
new Item {Id = 2, ParentId = null},
new Item {Id = 3, ParentId = 1}
};
var list2 = new List<Item>
{
new Item {Id = 3, ParentId = 1}
};
var result = list2.Union(list1.Where(l1 => list2.All(l2 => l2.ParentId != l1.Id)));
GetKeyFromUser("\nDone! Press any key to exit...");
}
}
)时提供错误处理程序即可。
tap()