## 獲取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>
~~~