gpt4 book ai didi

javascript - 如何获取响应 header - 获取响应 header 的问题

转载 作者:行者123 更新时间:2023-12-01 00:35:06 24 4
gpt4 key购买 nike

我在响应 header 中传递分页信息,但无法在 Angular 8 中使用以下方法。我一直收到 0 但响应显示不同的值。谁能告诉我我哪里出错了?

app.service.ts

  indexBooking(perPage: number, page: number) {
return this.http
.get<any[]>(`${this.apiBaseUrl}/prices`, {
params: formatParameters({perPage, page}),
observe: 'response',
})
.pipe(
map((res) => ({
max: Number.parseInt(res.headers.get('x-total-count'), 10) || 0,
index: Number.parseInt(res.headers.get('x-next-page'), 10) || 0,
page: Number.parseInt(res.headers.get('x-per-page'), 10) || 20,
items: res.body,
})),
);
}

app.component.ts

ngAfterViewInit() {
merge(this.paginator.page)
.pipe(
startWith({}),
switchMap(() => {
return this.bookingService.indexBooking(this.paginator.pageSize, this.paginator.pageIndex);
}),
map((data) => {
console.log('data', data);
this.resultsLength = data.items.length;
return data.items;
}),
catchError(() => {
return observableOf([]);
}),
)
.subscribe((data) => (this.data = data));
}

响应标题图像

enter image description here

最佳答案

您需要在后端设置 Access-Control-Expose-Headers 以公开您的自定义响应 header 。

取自 Mozilla Docs on Access-Control-Expose-Headers :

The Access-Control-Expose-Headers response header indicates which headers can be exposed as part of the response by listing their names.

By default, only the 6 CORS-safelisted response headers are exposed:

Cache-Control, Content-Language, Content-Type, Expires, Last-Modified, Pragma

If you want clients to be able to access other headers, you have to list them using the Access-Control-Expose-Headers header.

因此,您必须对后端进行编码以设置Access-Control-Expose-Headers,以便它返回:

Access-Control-Expose-Headers:x-total-count、x-next-page、x-per-page

或者

访问控制公开 header :*

这是一个简单的 stackblitz为了供您探索,您可以在 StackBlitz 的控制台中观察到我可以检索 Etag header ,但不能 X-Frame-Options 因为 https://api.github.com已设置 Access-Contorl-Expose-Headers 来公开 Etag,但不公开 X-Frame-Options:

enter image description here

关于javascript - 如何获取响应 header - 获取响应 header 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58196204/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com