Issue
I use a Laravel API and an Angular service to make my application. I'm doing a pagination for my array with Angular and I make a HTTP request with this function in my service :
getPagination(page): Observable<any> {
return this.httpClient.get<any>(environment.apiBaseUrl + 'test/?page=' + page);
}
When I trigger it I get the data with the pagination but when I want to get another page in prod I get a 404 error because it uses an URL that I don't even give.
I'm supposed to have this url:
http://prodUrl/back/api/test?page=2
But I get this :
http://prodUrl/api/test?page=2
Here is the function in the Laravel API
public function getAll()
{
$stuff = MyStuff::orderByDesc('updated_at')->paginate(50);
return response()->json($stuff, 200);
}
The route:
Route::get('test', [Controller::class, 'getAll']);
When I make a console.log()
of the environment.apiBaseUrl
in the first call to get the first page I have the right url and same thing for the content:
current_page: 1
first_page_url: "http://prodUrl/back/api/test?page=1"
from: 1
last_page: 3
last_page_url: "http://prodUrl/back/api/test?page=3"
links: (5) [{…}, {…}, {…}, {…}, {…}]
next_page_url: "http://prodUrl/back/api/test?page=2"
path: "http://prodUrl/back/api/test"
per_page: 50
prev_page_url: null
to: 50
total: 146
So why the HTTP request doesn't use it ?
Solution
I found a way to get the url I need by passing the url of the current page then use substring()
to remove the page number to add the paramter like below:
let urlSplit = url.substring(0, url.indexOf('=') + 1);
return this.httpClient.get<any>(urlSplit + page);
Answered By - leri
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.