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

lxk's avatar
Level 1

Ajax Request return Method not Allowed

Hey,

i searched in the discussions for a while, but i couldn't find any solution for me.

I want to retrieve data from an ajax request but the status is in any way 405 (Method not Allowed)

My Code is:

The Template:

@csrf
    <div class="form-group {{ $errors->has('product_id') ? ' has-warning' : '' }}">
        <label>Produkt</label>
        <select name="product_id" id="product_id" class="form-control">
            <option>-- Bitte auswählen --</option>
            @foreach($products as $product)
                <option id="{{$product->id}}" value="{{$product->id}}">{{$product->name}}</option>
            @endforeach
        </select>
    </div>

    <div class="form-group">
        <button type="submit" class="btn btn-primary">Hinzufügen</button>
    </div>
</form>

The ajax request:

<script>

    var product_id = $("#product_id").val();

    $('#product_id').change( function() {
        $.ajax({
            type:       'POST',
            data:       {
                product_id: product_id,
                _method: 'PUT',
                _token: '{{ csrf_token() }}'
            },
            url:        '/ajax/getData/papers',

            cache: false,
            success:function(response) {
                alert(response);
            }
        });
    });
    
</script>

The Route:

Route::post('/ajax/getData/papers', 'HelperController@getPapers');

The function in my Controller:

public function getPapers(Request $request) {
    $product_id = $request->product_id;

    $product = Product::findOrFail($product_id);

    return = view('admin.tiles.select', [
        'papers' => $product->papers(),
    ])
        ->render();
    
}

I hope you can find any mistakes... i'm at the end of my ideas.

best regards

0 likes
16 replies
Cronix's avatar

You're using a put request from ajax on a post route.

_method: 'PUT',
Route::post('/ajax/getData/papers', 'HelperController@getPapers');
lxk's avatar
Level 1

Yeah Thank you for the quick answer, but thats a false copy from my testing. There is no changes if i changed this like that:

<script>

    var product_id = $("#product_id").val();

    $('#product_id').change( function(e) {
        e.preventDefault();
        $.ajax({
            type:       'POST',
            data:       {
                product_id: product_id,
                _method: 'POST',
                _token: '{{ csrf_token() }}'
            },
            url:        '/admin/ajax/getData/papers',

            cache: false,
            success:function(response) {
                alert(response);
            }
        });
    });

</script>

Or i give it away completly

Cronix's avatar

just remove

_method: 'POST',

It's already a post request

type:       'POST',

Check your browsers dev tools. Is it still using the PUT method when sending that ajax request?

Also, make sure you force-refresh your browser so it will reload your js instead of load it from cache.

Cronix's avatar

Is this route within a route group that has a prefix of admin?

Route::post('/ajax/getData/papers', 'HelperController@getPapers');

if not, the url is /ajax/getData/papers, not /admin/ajax/getData/papers`

lxk's avatar
Level 1

yes, sorry i posted it a little bit easy for the understandig...

is in a prefix group like this -->

Route::prefix('admin')->group(function () { Route::post('/ajax/getData/papers', 'HelperController@getPapers'); });

jlrdw's avatar

If you would skip using a special route groups route prefixes and all that stuff and just use normal regular web routes with Ajax it works so much smoother. I don't know how many post I've seen lately with people have had trouble with this.

lxk's avatar
Level 1

Yeah, i also tried this with normal routes, without a prefix or something. I also tried to exept the url for CSRF protection... but no possibility to get this things running

Cronix's avatar

@lxk Please post all of your route file.

Put this as the very first route in your web.php route file. Don't put it in a group, just put it by itself as the first thing.

Route::post('/ajax/getData/papers', 'HelperController@getPapers');

Then have /ajax/getData/papers be the url for your ajax call.

This might not have anything to do with it, but your js should be all wrapped in a domready event.

<script>
$(function() {
    // the rest of your code
});
</script>

If your js is in a view file (I'm assuming it is since you're using blade syntax to get the token), then run php artisan view:clear to clear the view cache. It's really best to have js in separate js files though...

lxk's avatar
Level 1

okay, so here is the complete route file web.php

at the very first the ajax request route...

Auth::routes();

Route::post('/ajax/getData/papers', 'HelperController@getPapers');

Route::get('/', 'FrontendController@index'); Route::get('products/{product_name}', 'FrontendController@product');

Route::prefix('admin')->group(function () { Route::get('/', 'AdminController@index'); //CUSTOMER Route::get('/customer', 'CustomerController@index'); Route::get('/customer/sync', 'CustomerController@SyncCustomers'); Route::get('/customer/{id}/edit', 'CustomerController@editCustomer'); });

but the response is 404...

thats in the template

    var product_id = $("#product_id").val();

    $('#product_id').change( function() {
        $.ajax({
            type:       'POST',
            data:       {
                product_id: product_id,
                _token: '{{ csrf_token() }}'
            },
            url:        '/ajax/getData/papers',
            cache: false,
            success:function(response) {
                alert(response);
            }
        });
    });

</script>
Cronix's avatar

Did you do the other 2 things I suggested? Wrap the js in a domready event and php artisan view:clear?

lxk's avatar
Level 1

yes, same result...

jlrdw's avatar

I want to retrieve data from an ajax request

retrieve is usually a get method

https://laracasts.com/discuss/channels/guides/jquery-get-response

Are you trying to post or put data, or are you trying to get a response back with some data?

if you take this one baby step at a time really this stuff is not hard at all.

return = view

you don't render a view like that in Ajax that's a normal PHP way not the Ajax way.

you really need to take some YouTube basic videos on Ajax post and get it will help you so much.

lxk's avatar
lxk
OP
Best Answer
Level 1

Thanks for the tips!

My solution is now:

I make a get request and return a blade template like this:

$("#product_id").change(function (e) { e.preventDefault(); var product_id = $("#product_id").val(); var url = window.location.href+'/'+product_id; $('#add_product_form').fadeOut(1700);
        $('#add_product_form').load(url, function() {
            $('#add_product_form').fadeIn(1000);
        });
    });
</script>

And my Controller is:

public function addProductStep($order_id, $product_id) {

    $products = Product::where('published', 1)->get();
    $product = Product::findOrFail($product_id);


    return view('admin.order.tiles.add_product_form', [
        'products' => $products,
    'product' => $product,
    ]);
}
1 like

Please or to participate in this conversation.