在使用koa-body 作文件上传的时候,发现使用DELETE请求时,request.body中的内容为空对象{}node
//code... const Koa = require('koa'); const koaBody = require("koa-body"); app.use(koaBody({ multipart: true, formidable: { maxFileSize: 200 * 1024 * 1024 } })) //...code
查阅官方文档找到缘由
jquery
strict {Boolean} DEPRECATED If enabled, don't parse GET, HEAD, DELETE requests, default truegit
strict 参数:若是启用,则不解析GET,HEAD,DELETE请求,默认为truegithub
//...code app.use(koaBody({ multipart: true, strict:false,//设为false formidable: { maxFileSize: 200 * 1024 * 1024 } })) //...code
// 前端请求(jquery) $.ajax({ url:`${baseUrl}/xxx`, type:"DELETE", headers:{ "content-type":"application/json" }, data:{ name:"小明", age:18 } }).then(res=>{ console.log(res); }) // 后端处理函数部分 const fn_testDelete=async(ctx,next)=>{ const {name,age}=ctx.request.body; console.log(name,age);//小明 18 ctx.response.body={ code:200, errMsg:"OK" } }