简体   繁体   English

Koa路由器看不到后身?

[英]Koa router not seeing post body?

Testing koa.js and fetch on the client side. 测试koa.js并在客户端获取。 I am trying to post some data on the server. 我正在尝试在服务器上发布一些数据。 I am getting undefined with the code below. 我用下面的代码变得不确定。 I am unsure why. 我不确定为什么。

I have inspected this , this.request , this.req , etc and I cannot find my data from the client req? 我已经检查过thisthis.requestthis.req等,但是找不到客户端req的数据?

Server 服务器

import koaRouter from 'koa-router'

// Post route
router.post('/api/upload', function *(next) {
  console.log('UPLOAD WORKS', this.req.body)
  this.status = 200
})

app
  .use(bodyParser())
  .use(router.routes())
  .use(router.allowedMethods())

The console gives me undefined. 控制台给我未定义。

Client 客户

fetch('/api/upload', {
  method: 'post',
  body: 'Hello'
}).then(function(response) {
  console.log(response)
}).catch(function(err) {
  // Error :(
});

You should change your client fatch to use a 'FormData' object to wrap the string. 您应该更改客户端句柄,以使用“ FormData”对象包装字符串。

var formData = new FormData();
formData.append('json', 'Hello');

fetch('/api/upload', {
  method: 'post',
  body: formData
}).then(function(response) {
  console.log(response)
}).catch(function(err) {
  // Error :(
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM