简体   繁体   English

惯性 - 重新加载使用 POST 创建的页面 url 显示 GET 405(不允许的方法)

[英]Inertia - Reloading page url created with POST shows GET 405 (Method Not Allowed)

I want to load a new vue component and show its id in the URL .我想加载一个新的vue component并在URL中显示它的id 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) :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?我了解为什么会发生此错误,但是如何实现在新url中显示id而不会出现此错误的目标?

Edit : 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]);
});

Probably you needed to use GET Request instead of POST Request.可能您需要使用 GET 请求而不是 POST 请求。 If yes, then just add to url your id and then pass the GET method to param:如果是,那么只需将您的 id 添加到 url,然后将GET method传递给参数:

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 :对于 Laravel 中的后端 url 保持原样,但收到的参数只有$id并且方法需要更改为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.当您尝试通过 GET 方法加载 POST 方法的 url 时会出现405 (Method Not Allowed) ,反之亦然。

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).所以首先当你点击的时候,post 方法工作正常,但是当你重新加载页面时,它算作 GET 方法(就像直接打开页面一样)。 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).因此,收到 405 错误是公平的,如果您能够根据需要使用 GET 方法,则使用 GET 方法(如果您可以在用户直接访问页面而没有发布任何表单的情况下)。

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))如果没有,那么您可能首先在不同的路由中发布请求,然后将用户重定向到另一条路由(以防止在重新加载页面时重新发送表单,而是打开带有一些表单数据的另一个页面(当然它必须保存在某个地方,例如会话或分贝))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM