Laravel believes jQuery is always a GET request method

Laravel believes jQuery sends a GET request, even if it is not.

I confirmed this with the function request()->method().

Controller function:

public function fields($vertical_id = null, $lead_id = null)
{
    echo request()->method();
}

JQuery

$(document).on('change', '#vertical_id', function () {
    $.post('http://myapp.local/leads/fields/1/2', $(this).closest('form').serialize(), function (data) {
        console.log(data);
    });
});

Route:

Route::any('leads/fields/{vertical_id?}/{lead_id?}', 'LeadController@fields')->name('leads.fields');

Notice what I had to use any, because when I tried to use post, he threw an exception that excluded the method. My console shows GETevery time I make this request.

How to get Laravel to implement jQuery sends POST, not GET?

+4
source share
1 answer

Delete the name in the last of the routes.

Route::any('leads/fields/{vertical_id?}/{lead_id?}', 'LeadController@fields');

Try this code in route.php file

0
source

Source: https://habr.com/ru/post/1693006/


All Articles