javascript 请求正文在 KOA 中未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22148087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 22:33:16  来源:igfitidea点击:

Request Body is undefined in KOA

javascriptjqueryajaxnode.jskoa

提问by MBehtemam

I have KOALike below :

我有KOA,如下所示:

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 :

然后通过 jquery ajax 发送 post reqeust :

 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.reqi 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.

但是在 koa 中,this.req我找不到我的数据。在 google chrome 开发者工具中,我可以看到标题,并且一切正常,但我在 koa 中看不到它。

Update

更新

the correct is :

正确的是:

   function *getit(){
 console.log(this.request.body); //undefined
}

回答by wrentech

Interesting, I came across the same error, but my issue was the ordering of app.use() statements. The below does NOT work for me, and returns 'undefined' for this.request.body:

有趣的是,我遇到了同样的错误,但我的问题是 app.use() 语句的顺序。以下对我不起作用,并为 this.request.body 返回“未定义”:

var koa = require('koa');
var router = require('koa-router');
var bodyParser = require('koa-body-parser');

var app = koa();
app.use(router(app));
app.use(bodyParser());

app.post('/login', function *() {
    this.response.body = this.request.body;
});

app.listen(3000);

However, if I reverse the order of the two app.use() statements to the following:

但是,如果我将两个 app.use() 语句的顺序颠倒为以下:

app.use(bodyParser());
app.use(router(app));

I can then see the elements in this.request.body. I'm using koa v0.5.5, koa-body-parser v1.0.0, koa-router v3.1.2, and node v0.11.12.

然后我可以看到 this.request.body 中的元素。我正在使用 koa v0.5.5、koa-body-parser v1.0.0、koa-router v3.1.2 和节点 v0.11.12。

回答by satanas

I would use the co-bodymodule instead (the same module used internally by koa-body-parser). The code should looks like:

我会改用co-body模块(与 koa-body-parser 内部使用的模块相同)。代码应如下所示:

var koa = require('koa');
var _ = require('koa-route');
var parse = require('co-body');

var app = koa();

app.use(function* (next) {
  this.req.body = yield parse(this);
  yield next;
});

app.use(_.post('/login', function* () {
    this.body = this.req.body;
}));

app.listen(3000);

The var body = yield parse(this);line does the trick, the parse method will try to parse any request but you can replace it with parse.json, parse.formor parse.textin case you need more control

var body = yield parse(this);行可以解决问题, parse 方法将尝试解析任何请求,但您可以将其替换为parse.jsonparse.form或者parse.text如果您需要更多控制

回答by Rei Dien

koa uses middleware in the order you initialized them. so if you insert the router before the bodyparser it will not work. since your request wont pass to the parser before it gets to the router middleware, the request wont be parsed and koa will ignore the request.body. so the ordering is really important.

koa 按照你初始化的顺序使用中间件。因此,如果您在 bodyparser 之前插入路由器,它将无法正常工作。由于您的请求在到达路由器中间件之前不会传递给解析器,因此不会解析该请求并且 koa 将忽略 request.body。所以顺序真的很重要。

回答by MBehtemam

Solution

解决方案

Acording to comment , the correct for me is this.request.bodyand this.req.bodyis not correct.

Acording发表评论,正确的对我来说this.request.bodythis.req.body是不正确的。