如何获得响应 headers - 获得响应的问题 headers
How to get response headers - Issue with getting response headers
我在响应中传递分页信息 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));
}
回复Header图片
您需要在后端设置 Access-Control-Expose-Headers
以公开您的自定义响应 headers。
取自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
使其 returns:
Access-Control-Expose-Headers: x-total-count, x-next-page, x-per-page
或
Access-Control-Expose-Headers: *
这是一个简单的 stackblitz for you to explore, you can observe in the StackBlitz's console that I can retrieve Etag headers but not X-Frame-Options because https://api.github.com 已设置 Access-Contorl-Expose-Headers
以公开 Etag 但未公开 Etag X-Frame-Options:
我在响应中传递分页信息 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));
}
回复Header图片
您需要在后端设置 Access-Control-Expose-Headers
以公开您的自定义响应 headers。
取自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
使其 returns:
Access-Control-Expose-Headers: x-total-count, x-next-page, x-per-page
或
Access-Control-Expose-Headers: *
这是一个简单的 stackblitz for you to explore, you can observe in the StackBlitz's console that I can retrieve Etag headers but not X-Frame-Options because https://api.github.com 已设置 Access-Contorl-Expose-Headers
以公开 Etag 但未公开 Etag X-Frame-Options: