0x001
V2EX  ›  Node.js

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

  •  
  •   0x001 · Jun 28, 2014 · 7120 views
    This topic created in 4512 days ago, the information mentioned may be changed or developed.
    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
    Supplement 1  ·  Jun 30, 2014
    找到原因了,因为我post的是一个raw的字符串,Express默认只支持json与x-www-form-urlencoded.
    7 replies  •  2014-06-30 10:30:50 +08:00
    coolicer
    coolicer
    Jun 28, 2014
    1
    你加了body-parser没有
    coolicer
    coolicer
    Jun 28, 2014
    2
    83053187 新手群欢迎你。
    0x001
    0x001
    OP
    Jun 28, 2014
    3
    @coolicer 我用express创建的工程,加了body-parser的
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded());
    WildCat
    WildCat
    Jun 28, 2014 via iPhone
    4
    试试
    req.param('name')
    breeswish
    breeswish
    Jun 28, 2014
    5
    req.body: body
    req.param: query string
    RaymondYip
    RaymondYip
    Jun 29, 2014 via iPad
    6
    Req.query
    acecode
    acecode
    Jun 30, 2014
    7
    要不console.log( util.inspect(req, 3, 4, true) )看看?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4257 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 45ms · UTC 01:08 · PVG 09:08 · LAX 17:08 · JFK 20:08
    ♥ Do have faith in what you're doing.