I have KOA Like below :
var koa = require('koa'),
bodyParser = require('koa-body-parser'),
router = require('koa-router'),
app = koa();
app.use(router(app));
app.use(bodyParser());
app.post('http://localhost/get',getit);
function *getit(){
console.log(this.req.body); //undefined
}
and then send a post reqeust via jquery ajax :
var xhr = $.ajax({
type: 'POST',
dataType: 'json',
contentType: 'application/json',
url: 'http://localhost/getit',
data: {"name":"me"},
success: function(response) {
}
});
but in koa and in this.req
i cant find my data. in google chrome developer tools i can see the header and everything send ok but i cant see it in koa.
Update
the correct is :
function *getit(){
console.log(this.request.body); //undefined
}
bodyParser = require('koa-body-parser),
copy paste issue? that's an unterminated literal string there – Farmannrequest
instead ofreq
like it used to be in Express. Soconsole.log(this.request.body);
should solve your problem. – Ford