表单提交中碰到的一些坑
问题描述:
在使用 laravel 框架进行 ajax 提交的时候,一直报错 419,想了下肯定是 csrf_token 问题。仔细看了下官方文档:
javascript 的 header 头和 meta 标签都已经添加了,一直找不到问题出在哪里。
<meta name="csrf-token" content="{{ csrf_token() }}">
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
于是试用了正常情况下的表单提交,也是认证不通过。但这个时候有了个明确的错误信息提示:
The page has expired due to inactivity. Please refresh and try again.
在网上找了下,马上就找到了解决方案:
May have something to do with the storage_path not being writable. This is where it stores session data regarding tokens if you're using file based sessions.
As stated in the comments, the session driver was set to array. Array is for testing only. Since data is not persisted, it will not be able to compare the token on the next request.
The array driver is used during testing and prevents the data stored in the session from being persisted.
总结:
- 多看看官方文档。
- 尽量使用「官方推荐的开发环境」。
附言:
以前我也一直用 homestead 进行开发,由于每次都要启动,电脑配置又有点差,加上用的 ubuntu OS。
所以觉得没必要再用 homestead,但是下来走了一些坑。就比如这个问题,花了我快一个小时的时间。
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: