Back

Problem with route get method.


Edoardo Di Paolo posted 2 months ago

Hi!

I'm having a problem with a Route::get. I've to manage some articles, so I created a get route like that:

Route::get('spot/{id}', '[email protected]');

In the function getSpot I check if the id parameter is an integer, if the spot exists and so on. If all is ok, I return a view and in this view I include a .js that has some ajax request like a search. But when I send the request to my search post controller, I have "405 method not allowed". I saw that if I remove the {id} parameter from the route, the ajax request works very well.

I setted up the CSRF_TOKEN everywhere (meta tag, and ajaxSetup in js file).

I'm new in laravel, and I've no idea why..someone could help me?

Md.Miraj Khan replied 2 months ago

Can you show your ajax code?

Edoardo Di Paolo replied 2 months ago

Hey, yes it is:

$("#searchInput").on('keyup', function(e){
        var query = $(this).val();
        if(query.length > 0) {
            $(".searchDropdown").empty();
            var isProcessing = false;
            if(!isProcessing) {
                $.ajax({
                    method: 'POST',
                    url: 'doSearch',
                    data: {
                        query: query
                    },
                    beforeSend: function () {
                        isProcessing = true;
                    },
                    success: function (data) {
                        $(".searchDropdown").empty();
                        if (data.status === "success") {
                            $.each(data.spot_list, function (i, v) {
                                $(".searchDropdown").prepend("<li class=\"dropdown-item\" style=\"cursor: pointer;\"><a class=\"blackColor\" href=\"asd\"><b>#" + v.id + "</b> " + v.text + "</a></li>");
                            });
                            if (data.othersResults == true) {
                                $(".searchDropdown").append("<li class=\"dropdown-item\" style=\"cursor: pointer;\"><a class=\"blackColor\" href=\"asd\"><b>Altri risultati...</b></a></li>");
                            }
                            $(".searchDropdown").show();
                            isProcessing = false;
                        }
                    }
                });
            }
        } else {
            $(".searchDropdown").hide();
        }
        return false;
    });

But this code in my home works well.

This is the home route:

Route::get('home', function(){
stuff to check if userlogged
});
Md.Miraj Khan replied 2 months ago

Problem is in your AJAX method . Your route accept GET request ,but you are trying to POST .

 $.ajax({
   method: 'POST',  // change to GET
 })

Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us