【问题标题】:SvelteKit form actions and redirectSvelteKit 表单操作和重定向
【发布时间】:2023-01-12 19:26:10
【问题描述】:

在我的 SvelteKit 应用程序中,我有一个使用表单操作的表单:

<form action="/login?/logout" method="POST">
    <button type="submit">Logout</button>
</form>

在此操作中,最后一步是重定向:

export const actions: Actions = {
    logout: async (event) => {
        // ...
        throw redirect(307, '/login');
    }
}

这是按我的预期工作的。更新到 SvelteKit 1.0.0 后,行为发生了变化:操作被调用(和以前一样)但是在重定向时我收到以下错误:

错误:未找到名称为“默认”的操作

有什么想法需要改变或应该如何改变吗?

【问题讨论】:

    标签: sveltekit


    【解决方案1】:

    Status code 307 will not change the method and body的请求,所以浏览器会尝试POST /login

    您可以将状态代码更改为303,而不是将方法更改为GET

    export const actions: Actions = {
        logout: async (event) => {
            // ...
            throw redirect(303, '/login');
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-11
      • 1970-01-01
      • 2023-01-14
      • 1970-01-01
      • 2021-04-17
      • 2017-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多