node.js – Verwendung von koa2 + koa-router, aber der request.body der Post-Anfrage kann nicht abgerufen werden
我想大声告诉你
我想大声告诉你 2017-06-17 09:16:49
0
1
1277

Xiaobai hat beim Absenden eines Formulars über „/“ eine Post-Anfrage gesendet, aber der von „/link“ zurückgegebene Text enthält ein „leeres Objekt“. request.body{}

const koa = require('koa')

const router = require('koa-router')()

const koaBody = require('koa-body')()

const bodyParser = require('koa-bodyparser')

const app = new koa()

app.use(bodyParser())

app.use(async (ctx, next) => {
  const start = new Date()
  await next()
  const ms = new Date() - start
  console.log(`${ctx.method} -> ${ctx.url} - ${ms}ms`)
})

router.get('/',async (ctx, next) => {
  await next()
  ctx.body = `<form action='/link' method='post'><input type="text" ><input type="submit" value="OK"></form>`
})

router.get('/hello/:name', async ctx => {
  let name = ctx.params.name
  ctx.body = `<h1>Hello, ${name}!</h1>`
})

app.use(router.routes())

router.post('/link', async (ctx, next) => {
  
  ctx.response.body = {
    code:'0',
    description: 'ok',
    result: ctx.request.body
  }
})


// app.use(async ctx => {
//    ctx.body = 'hello koa2'
// })

app.listen(3000)
我想大声告诉你
我想大声告诉你

Antworte allen(1)
typecho

ctx.body = `<form action='/link' method='post'><input name="name" type="text" ><input type="submit" value="OK"></form>`

输入框要有name才可以

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!