quick update, please note in the ajax that the url should be
url: 'users/ajax_filter',
I missed updating this in my "abstraction"
Problem is still occurring
I've edited the original post above to reflect this
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi folks,
I am trying to convert one of my ajax POST requests into an ajax GET request, reason being is I am only really passing two things into the function, and am using it to return data not submit or update anything on the server.
As a POST request everything works fine, see below (some variable names and what not abstracted but the structure remains the same
web.php routes file
Route::post('/users/ajax_filter', 'UserController@ajax_filter')->name('ajax_filter');
Ajax function
function ajaxFilter(element) {
$.ajax({
type: "POST",
url: "/users/ajax_filter",
datatype: "text",
data: {
'_token': token,
'variable': $(element).val(),
},
success: function(profiles) {
//do stuff
}
});
}
User Controller function
public function ajax_filter(Request $request)
{
//does stuff
return json_encode($stuff);
}
in both my route and my ajax function the first thing I did was swap out post for get.
Route::get('/users/ajax_filter', 'UserController@ajax_filter')->name('ajax_filter');
function ajaxFilter(element) {
$.ajax({
type: "GET",
url: "/users/ajax_filter",
datatype: "text",
data: {
'_token': token,
'variable': $(element).val(),
},
success: function(profiles) {
//do stuff
}
});
}
This is leading to a 404 error
Failed to load resource: the server responded with a status of 404 (Not Found)
What looks to be happening to my eyes is that the ajax isn't sure where to submit to because the get query parameters are being appended to the string.
When I inspect the network tab in chrome I can see the GET request is trying to reach: http://example.com/users/ajax_filter?_token=BIGLONGSTRING&variable=value
Contrast that with my POST request which was simply hitting: http://example.com/users/ajax_filter
I've looked online for some guidance on this but can't quite narrow in on the issue, is there something I need to do with my route if its accepting query parameters? Or is there something additional that need to be added to my ajax to ensure I can hit the right route?
Any insight is great appreciated, thanks!
Please or to participate in this conversation.