Axios extra slash issue when using with Laravel
P粉298305266
P粉298305266 2024-02-21 14:41:04
0
1
359

It's time for me to get some help because I don't understand the problem at all and spent 6 hours on it and got nowhere :-(

I have an Axios GET request whose last parameter may be empty.

axios.get(this.fetchAllUsersRoute + '/' + this.status + '/' + this.pagination + '/' + this.search);

My Laravel route:

Route::get('/fetch-users/{status}/{pagination}/{search?}', 'MyController@fetchUsers')->name('fetch-users');

When this.search is empty, I get this:

Request URL: https://mywebsite.dev/fetch-users/0/1/
Request Method: GET
Status Code: 301 Moved Permanently (from disk cache)

Every request will be redirected here:

https://mywebsite.dev/fetch-users/0/1

When the value is left empty, the last / slash seems to cause a redirect.

As soon as I removed it, the problem stopped...no redirects.

Any idea how to make the last slash disappear if the last value is empty?

Thanks.

P粉298305266
P粉298305266

reply all(1)
P粉269530053

Your request is incompatible with the route. You can try to create a request link like below.

var fetchAllUsersRoute = "https://mywebsite.dev"
var status = 'status'
var pagination = 'pagination'
var search

var url = fetchAllUsersRoute + '/' + status + '/' + pagination + (search != null ? ('/' + search) : '')

console.log(url)
// "https://mywebsite.dev/status/pagination"

search = 'search'

url = fetchAllUsersRoute + '/' + status + '/' + pagination + (search != null ? ('/' + search) : '')

console.log(url)
// "https://mywebsite.dev/status/pagination/search"
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template