NIUCLOUD是一款SaaS管理后台框架多应用插件+云编译。上千名开发者、服务商正在积极拥抱开发者生态。欢迎开发者们免费入驻。一起助力发展! 广告
## 获取post提交的数据,必须先生成csrf秘钥 ~~~ this.ctx.request.body ~~~ egg.js中要post数据必须先签名认证 ## 1.访问首页先生成csrf秘钥 ~~~ const Controller = require('egg').Controller; class HomeController extends Controller { async index() { /* 用户访问这个页面的时候生成的秘钥 */ console.log(this.ctx.csrf) await this.ctx.render('index',{ csrf:this.ctx.csrf }) } /* 接受post提交的数据 */ async add(){ this.ctx.body = "add" console.log(this.ctx.request.body) } } module.exports = HomeController; ~~~ ## 2.在index.html中使用这个秘钥,才能在后台获取post数据 ~~~ <form action="/add?_csrf=<%= csrf%>" method='post'> <p>用户名: <input type="text" name="username"></p> <p>密码: <input type="password" name="password"></p> <input type="submit" value="提交"> </form> ~~~