get请求获取地址栏请求参数参数
http://localhost:7001/role?roleId=1&number=1
'use strict';
const { Controller } = require('egg');
class RoleController extends Controller {
async index() {
const { ctx } = this;
ctx.body = {
code: 0,
data: {
list: [
{
name: 'ming',
key: 'name',
},
],
},
query: ctx?.query,
msg: 'hello',
ctx,
};
}
}
module.exports = RoleController;
post请求获取请求参数
http://localhost:7001/roles?userNane=Yan&roleId=2
'use strict';
const { Controller } = require('egg');
class RoleController extends Controller {
async index() {
const { ctx } = this;
ctx.body = {
code: 0,
data: {
list: [
{
name: 'ming',
key: 'name',
description: 'desc',
},
],
},
query: ctx?.query,
msg: 'hello',
};
}
}
module.exports = RoleController;
关于使用浏览器发送post请求(火狐浏览器)
点击网路面板下的请求,点击重发即可在左侧修改请求方法和请求地址,然后发送
egg发送post请求出现403解决
发现是出现403,服务器组阻止访问,原因是egg 框架内置了安全系统,默认开启防止 XSS 攻击 和 CSRF 攻击。
在Security的默认拦截器里,默认会开启CSRF处理,判断请求是否携带了token,如果没有就拒绝访问。并且,在请求为(GET|HEAD|TRACE|OPTIONS)时,则不会开启。
//在config.default.js配置文件添加
config.security = {
csrf: {
enable: false,
},
};