表单提交中碰到的一些坑

问题描述:

在使用 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.

总结:

  1. 多看看官方文档。
  2. 尽量使用「官方推荐的开发环境」。

附言:
以前我也一直用 homestead 进行开发,由于每次都要启动,电脑配置又有点差,加上用的 ubuntu OS。

所以觉得没必要再用 homestead,但是下来走了一些坑。就比如这个问题,花了我快一个小时的时间。

本作品采用《CC 协议》,转载必须注明作者和本文链接
do it now
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 2
Summer

尽量使用「官方推荐的开发环境」

哈哈,这绝对是经验之谈。

6年前 评论

自已配置的环境,速度快了很多,也是遇到这个坑.. 感谢分享

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!