express POST 解析失败
发布于 13 天前 作者 ChinaRedArmy1930 235 次浏览 来自 问答

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

<head> <meta charset="utf-8"> // <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta http-equiv="Content-Type" Content="application/json;Charset=utf-8"> <title>Login Form</title> <link rel="stylesheet" href="http://10.10.103.234/css/style.css"> <script type="text/javascript" src="http://10.10.103.234/js/1.js"></script> <script type="text/javascript" src="http://10.10.103.234/js/2.js"></script> <style> #fly { width: 50px; height: 20px; background-color: red; } </style> </head> <body>

用户登录

<form method="post" action="http://10.10.103.234:8000/index.html">

<input type="text" id="username" name="username" value="yxy" placeholder="用户名">

<input type="password" id="password" name="password" value="" placeholder="密码">

<input type="text" name="checkNum" id="checkNum" value="" placeholder="验证码">

<input id="submit" type="submit" name="commit" value="登录">

</form>
<div class="login-help">
</div>
</body> </html>

这是我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 回复
req.body.username
req.body.password
req.body.checknum

把这三个值打印一下

@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

req.body.checkNum

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

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

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

回到顶部