I'm passing pagination information in response header and unable to get using following method in angular 8. I'm getting 0
all the time but in response showing diffrent value. Can anyone let me know where I made mistake?
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));
}
Response Header Image
You need to set
Access-Control-Expose-Headers
in your backend to expose your custom response headers.Taken from Mozilla Docs on Access-Control-Expose-Headers:
Therefore, you have to code your backend to set
Access-Control-Expose-Headers
such that it returns:Access-Control-Expose-Headers: x-total-count, x-next-page, x-per-page
OR
Access-Control-Expose-Headers: *
Here's a simple 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 has set
Access-Contorl-Expose-Headers
to expose Etag but not X-Frame-Options: