Home > Net >  Inertia - Reloading page url created with POST shows GET 405 (Method Not Allowed)
Inertia - Reloading page url created with POST shows GET 405 (Method Not Allowed)

Time:05-11

I want to load a new vue component and show its id in the URL. This can be done like so:

<template>
  <button @click="submit">Load new page with id in url</button>
</template>

<script>
import {Inertia} from "@inertiajs/inertia";

const submit = () => {
    const id = 1
    Inertia.post('/admin/kreationen/bearbeiten/'   id, {id: id})
}
</script>

and on the laravel side (web.php):

Route::post('/admin/kreationen/bearbeiten/{id}', function (Request $request) {
    return inertia('backend/cms-gallery-edit', ['id' => $request->id]);
});

Problem is, when I am reloading the page, this error is shown:

GET http://127.0.0.1:8000/admin/kreationen/bearbeiten/1 405 (Method Not Allowed)

I understand why this error is happening but how would I achieve the goal of showing the id in the new url without getting this error?

Edit:

I don't know why my original approach didn't work yesterday but now it does. For anyone interested:

<inertia-link :href="'/admin/kreationen/bearbeiten/'   data['id']">
  <button>Load new page with id in url</button>
</inertia-link>

backend:

Route::get('/admin/kreationen/bearbeiten/{id}', function (Request $request) {
    return inertia('backend/cms-gallery-edit', ['id' => $request->id]);
});

CodePudding user response:

Probably you needed to use GET Request instead of POST Request. If yes, then just add to url your id and then pass the GET method to param:

const id = 1      
Inertia.visit('/admin/kreationen/bearbeiten/'   id, {method: 'get'})

And for Backend side in Laravel url stays as it is, but recieved parameter is only $id and method needs to change to GET:

    Route::get('/admin/kreationen/bearbeiten/{id}', function ($id) {
        return inertia('backend/cms-gallery-edit', ['id' => $id]);
    });

The 405 (Method Not Allowed) occurs when you try to load the POST methoded url via GET method and vice versa.

So first when you click, the post method is working fine, but when you reloading the page, it counts as GET method (just like direct opening the page). So recieving 405 error is fair, if you are able to use GET method to your needs, then use GET method (if you are ok with whenever user accesses the page directly without any form posted).

If not, then you might firstly do you POST requests in different route, then redirect user to another route (To prevent re-sending form when reloading the page, but to opening another page with some form data (ofcourse it must be saved somewhere like session or db))

  • Related