Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

the-bing_man's avatar

Ajax GET request not finding proper route

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!

0 likes
13 replies
the-bing_man's avatar

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

Cronix's avatar

Your ajax is sending to a different url than you have defined in the route. One is sending to /users/filter_profile, but the route is listening on /users/ajax_filter.

Tray2's avatar

These two needs to match

Route::post('/users/ajax_filter', 'UserController@ajax_filter')->name('ajax_filter');
url: "/users/filter_profiles",
Cronix's avatar

Try clearing the browser cache, as well as your route cache.

the-bing_man's avatar

@cronix

I've tried clearing my browser cache as well as other caches with artisan commands (including the route cache). Still no luck

jlrdw's avatar

Have you played around with the placement of the slashes.

url: 'petupdate',

Notice no leading slash.

This stuff takes a little trial and error sometimes.

By the way my Ajax post works perfect.

Make sure your token is included as mentioned above.

Cronix's avatar

You should post your whole routes file.

It would be good to show your current, updated code where you are actually using the GET route instead of post for all of this. You say you are, but all of your code is showing POST.

Cronix's avatar

Notice no leading slash.

@jlrdw You should start with a slash so it's not relative, since laravels routes aren't relative...

Snapey's avatar

You don't need to do anything to receive the query string

You do need to check that the URL as GET does not clash with any other routes.

You should also check in network tools in your browser, exactly what route is being hit.

You can also copy this URL to your browser address bar and access it without ajax.

For GET requests you can drop _token from your query string

Try php artisan route:list and carefully check the routes there against the URL called as observed in your network tools

jlrdw's avatar

Did, updated answer:

OP, for the sake of simplicity goto google an cut and paste this:

site:laracasts.com laravel jquery get request
or
site:laracasts.com laravel jquery get response  //whatever

And read. This stuff has been covered hundreds of times with great examples.

<script>

    $(function () {
        $("#testme").click(function (event)
        {
            event.preventDefault();

            var somevar = '1';
            $.ajax({
                url: 'testc',   // A ROUTE HERE
                type: 'GET',
                data: 'somevar=' + somevar,
                dataType: 'json',
                success: function (data) {
                    if (data.error) {
                        //handle the error here
                    } else {
                        alert(data.dogname);
                        $('#dogname').val(data.dogname);
                        $('#comment').val(data.comments);
                    }
                }
            });

        });


    });



</script>

Please or to participate in this conversation.