V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
0x001
V2EX  ›  Node.js

Express 4.2.0 post 为何不能解析参数呢。

  •  
  •   0x001 · 2014-06-28 08:59:56 +08:00 · 5408 次点击
    这是一个创建于 3561 天前的主题,其中的信息可能已经有所发展或是发生改变。
    router.post('/', function(req, res) {
    console.log(req.body);
    console.log(req.body.name);
    console.log(req.body.tel);
    res.send("ok");
    });


    下面是log:
    {}
    undefined
    undefined
    POST / 200 2ms - 2b
    第 1 条附言  ·  2014-06-30 10:47:43 +08:00
    找到原因了,因为我post的是一个raw的字符串,Express默认只支持json与x-www-form-urlencoded.
    7 条回复    2014-06-30 10:30:50 +08:00
    coolicer
        1
    coolicer  
       2014-06-28 09:09:34 +08:00
    你加了body-parser没有
    coolicer
        2
    coolicer  
       2014-06-28 09:10:21 +08:00
    83053187 新手群欢迎你。
    0x001
        3
    0x001  
    OP
       2014-06-28 09:18:51 +08:00
    @coolicer 我用express创建的工程,加了body-parser的
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded());
    WildCat
        4
    WildCat  
       2014-06-28 10:16:10 +08:00 via iPhone
    试试
    req.param('name')
    breeswish
        5
    breeswish  
       2014-06-28 10:21:24 +08:00
    req.body: body
    req.param: query string
    RaymondYip
        6
    RaymondYip  
       2014-06-29 23:35:14 +08:00 via iPad
    Req.query
    acecode
        7
    acecode  
       2014-06-30 10:30:50 +08:00
    要不console.log( util.inspect(req, 3, 4, true) )看看?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5439 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 08:49 · PVG 16:49 · LAX 01:49 · JFK 04:49
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.