0

Time for me to ask some help as I simply do not understand the issue, spent a good 6 hours on this, going nowhere :-(

I have an Axios GET request which may have the last parameter 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 the this.search is empty I am getting this:

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

It redirects to here on each request:

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

The last / slash seems to be causing a redirection when this value is left empty.

As soon as I remove it, the problem stops...no redirection.

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

Thank you.

1
  • Please provide the link you are requesting as an example. Commented May 16, 2022 at 22:17

1 Answer 1

2

Your request is incompatible with the route. You can try to create 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"
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for the reply. So this is how most people would do it when one of the GET parameters is(from time to time) empty? Using ternary operators? Is this common?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.