Home  >  Q&A  >  body text

Axios extra slash issue when using with Laravel

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粉298305266P粉298305266240 days ago280

reply all(1)I'll reply

  • P粉269530053

    P粉2695300532024-02-22 10:04:41

    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"

    reply
    0
  • Cancelreply