2016-02-11 3 views
0

У меня есть приложение-приложение, которое я хочу передать значение до сервера koa.Как передать значение Koa2

let data = new FormData() 
    data.append('json', JSON.stringify(token)) 

    fetch('/charge', { method: 'POST', body: data }) 
    .then((res) => { 
     return res.json() 
    }) 
    .then((json) => { 
     console.log('something wrong') 
     console.log(json) 
    }) 

и ниже мой код

const config = require('../config') 
const server = require('../server/main') 
const router = require('koa-router')() 
const parse = require("co-body") 

const port = config.server_port 

server.use(router.routes()) 

router 
    .post('/charge', function (ctx, next) { 
    console.log(ctx.request.body) 
    console.log('howyd') 
    ctx.body = "howdy" 
    }) 

сервер просто не может получить значение, проходящую вниз от клиента. Вы, ребята, знаете, что происходит?

ответ

0

Убедитесь, что вы используете парсер. Похоже, вы требуете его, но на самом деле не используете его. Что-то вроде этого (непроверено):

const config = require('../config') 
const server = require('../server/main') 
const router = require('koa-router')() 
const parse = require("co-body") 

const port = config.server_port 

server.use(router.routes()) 

router 
    .post('/charge', async (ctx, next) => { 
    let body = await parser.json(ctx.request) 
    console.log(body) 
    console.log('howyd') 
    ctx.body = "howdy" 
    }) 
+0

Все хорошие люди. Причина в том, что я не включил 'koa-bodyparser', поэтому koa не знал, как разбирать предстоящий запрос и поэтому всегда возвращает' undefined' – Well