[英]$page.props.csrf_token is undefiend (inertiajs)
i want to logout the user to NON inertia url. so i have to pass the csrf token
to a form with post methd.我想将用户注销到非惯性 url。所以我必须将
csrf token
传递给带有 post 方法的表单。
<!-- Authentication -->
<form
method="POST"
:action="route('logout')"
>
<input type="hidden" name="_token" :value="$page.props.csrf_token">
<jet-dropdown-link
colors="text-red-700 focus:bg-gray-200 hover:bg-gray-200"
as="button"
>
logout
</jet-dropdown-link>
</form>
but it gives me a 419 page expired
response.但它给了我一个
419 page expired
响应。
when i console.log("this.$page.props.csrf_token")
it shows undefined
.当我
console.log("this.$page.props.csrf_token")
它显示undefined
。
what should i do?我应该怎么办?
it was my bad sorry.我很抱歉。 i should have read the documentation carefully.
我应该仔细阅读文档。
so you can share the token with inertia middleware.因此您可以与惯性中间件共享令牌。
namespace App\Http\Middleware;
class HandleInertiaRequests extends Middleware
{
public function share(Request $request)
{
return array_merge(parent::share($request), [
'csrf' => csrf_token(),
]);
}
}
and then in inertia compenents access it throught:然后在惯性组件中访问它:
this.$page.props.csrf
You need to add @csrf under your "<form" and add您需要在“<form”下添加@csrf 并添加
<meta name="csrf-token" content="{{ csrf_token() }}">
in of your layout在你的布局中
the simplest way is using csrf_field
:最简单的方法是使用
csrf_field
:
<form>
{{ @csrf_field() }}
<!-- other fields -->
</form>
For anyone else who needs to find a way to pass the csrf token to inertia, one use case I have specifically is the need to have an export csv file button that sends an actual, non-AJAX HTTP request - so cant rely on axios' auto csrf feature - so I can return a file download.. what I did was in my controller where I populate $page.props, you can set an attribute to the value of the method 'csrf_token()'.. so -对于需要找到一种方法将 csrf 令牌传递给惯性的任何其他人,我有一个具体的用例是需要一个导出 csv 文件按钮,该按钮发送一个实际的非 AJAX HTTP 请求 - 所以不能依赖 axios'自动 csrf 功能 - 这样我就可以返回文件下载.. 我所做的是在我的 controller 中填充 $page.props,您可以将属性设置为方法 'csrf_token()' 的值.. 所以 -
Inertia::render( 'VueComponent', [ 'attribute_name' => csrf_token() ]); Inertia::render( 'VueComponent', [ 'attribute_name' => csrf_token() ]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.