跳到主要内容
CNode

express POST 解析失败

问答
CChinaRedArmy1930发布于 10 年前最后回复 10 年前
539260

我现在的问题是,使用express解析web浏览器反馈回答的数据,mode解析失败,打印为Object,但是监查端口却有数据,请指点! 这是我html代码:

// Login Form #fly { width: 50px; height: 20px; background-color: red; }
<div class="login-help">
</div>

这是我js代码: var express = require('express'); var app = express(); var bodyparser=require('body-parser'); var router = express.Router(); app.use(bodyparser.json()); app.use(bodyparser.urlencoded({ extended: true })); app.use(express.static('public')); app.use(express.static('views'));

app.get('/',function(req,res){ res.sendFile(/views/regin.html); });

app.post('/index.html',function(req,res){ console.log("yangxiaoyu test!!!"); console.log("post body:"+req.body); console.log("post query:"+req.query); });

var server=app.listen(8000,function(req,rse){ var host = server.address().address; var port = server.address().port; console.log('app listening at http://%s', port); });

这是打印: yangxiaoyu test!!! post body:[object Object] post query:[object Object]

这是端口数据: 0x0020: 5018 00ff e591 0000 7573 6572 6e61 6d65 P.......username 0x0030: 3d79 7879 2670 6173 7377 6f72 643d 7978 =yxy&password=yx 0x0040: 7926 6368 6563 6b4e 756d 3d26 636f 6d6d y&checkNum=&comm

可以看到端口是有数据的,解析有问题,以下是我npm list: ├─┬ [email protected] │ ├── UNMET DEPENDENCY qs@~0.6.6 │ └── UNMET DEPENDENCY raw-body@~1.1.2 ├─┬ [email protected] │ ├─┬ [email protected] │ │ ├─┬ [email protected] │ │ │ └── [email protected] │ │ └── [email protected] │ ├── [email protected] │ ├── [email protected] │ ├── [email protected] │ ├── [email protected] │ ├── [email protected] │ ├─┬ [email protected] │ │ └── [email protected] │ ├── [email protected] │ ├── [email protected] │ ├── [email protected] │ ├── [email protected] │ ├─┬ [email protected] │ │ ├── [email protected] │ │ └── [email protected] │ ├── [email protected] │ ├── [email protected] │ ├── [email protected] │ ├─┬ [email protected] │ │ └── [email protected] │ ├── [email protected] │ ├── [email protected] │ ├─┬ [email protected] │ │ ├── [email protected] │ │ └── [email protected] │ ├── [email protected] │ ├── [email protected] │ ├─┬ [email protected] │ │ ├── [email protected] │ │ ├─┬ [email protected] │ │ │ └── [email protected] │ │ └── [email protected] │ ├── [email protected] │ ├─┬ [email protected] │ │ └── [email protected] │ ├── [email protected] │ └── [email protected] ├─┬ [email protected] │ ├── [email protected] │ ├─┬ [email protected] │ │ ├── [email protected] │ │ ├── [email protected] │ │ ├── [email protected] │ │ └── [email protected] │ └── [email protected] └── [email protected]

请教各位,我哪里有问题?

查看回复

回复 (5)

A
ajaxQWER#510 年前

你哪里有问题?是你哪里不懂还是什么东西?麻烦描述清楚。 打印出来是 post body:[object Object] post query:[object Object] 是对的啊,你直接console.log(req.body)就能看到了

F
fantasticsoul#410 年前

或者你改写下你的写法: 1 console.log('post body:',req.body) 2 console.log('post body'+JSON.stringify(req.body));

L
liujavamail#310 年前

req.body.checkNum

直接打印出来的不知道是对象还是数组,因为太长了,显示不了,要得到具体的值,这么取出来才行

C
ChinaRedArmy1930#210 年前
引用 liujavamail把这三个值打印一下

@liujavamail -bash-4.1$ sudo /usr/local/node/bin/node app.js app listening at http://8000 yangxiaoyu test!!! post+req.body.username: yxy post+req.body.password: yxy post+req.body.checknum: undefined

好像有点意思,能解释一下么? 我看网上的一些代码都是直接打印req.body

L
liujavamail#110 年前
req.body.username
req.body.password
req.body.checknum

把这三个值打印一下

参与回复

登录后即可参与回复。登录