nuxt+koa2中的post请求使用koa-bodyparser 反而报错?

新手上路,请多包涵

准备用nuxt+koa搭建一个网站。在使用POST请求的时候,发现后台获取不到数据。然后根据网上的说法,使用了koa-bodyparser。然后问题就来了。


后台代码

 const bodyParser = require("koa-bodyparser");
 const app = new Koa();
 app.use(convert(bodyParser({})));
 app.use(navi.routes()).use(navi.allowedMethods());
router.post("/additem", async ctx => {
  return (ctx.body = { code: 200, result: "添加成功" });
});

前端代码

this.$axios.post("/api/navi/additem", {
          name: "this.name",
          password: "this.password"
        })
        .then(res => {
          console.log(res);
        })
        .catch();

后台报错

image.png

请求状态,请求失败(failed)

image.png

我尝试使用postman请求,居然是可以的。没有报错。我发现请求进入koa-bodyparser后,一直到了这句代码,这下我就抓瞎了。就很奇怪这个问题?

const str = await raw(inflate(req), opts);

阅读 1.8k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题