我现在的问题是,使用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> <section class=“container” > <div class=“login”> <h1>用户登录</h1> <form method=“post” action=“http://10.10.103.234:8000/index.html”> <p><input type=“text” id=“username” name=“username” value=“yxy” placeholder=“用户名”></p> <p><input type=“password” id=“password” name=“password” value="" placeholder=“密码”></p> <p><input type=“text” name=“checkNum” id=“checkNum” value="" placeholder=“验证码”></p> <p class=“remember_me”> </p> <p class=“submit” style=“text-align: center”><input id=“submit” type=“submit” name=“commit” value=“登录”></p> </form> </div>
<div class="login-help">
</div>
</section> <div style=“text-align:center;”> </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: ├─┬ body-parser@1.0.0 │ ├── UNMET DEPENDENCY qs@~0.6.6 │ └── UNMET DEPENDENCY raw-body@~1.1.2 ├─┬ express@4.14.0 │ ├─┬ accepts@1.3.3 │ │ ├─┬ mime-types@2.1.13 │ │ │ └── mime-db@1.25.0 │ │ └── negotiator@0.6.1 │ ├── array-flatten@1.1.1 │ ├── content-disposition@0.5.1 │ ├── content-type@1.0.2 │ ├── cookie@0.3.1 │ ├── cookie-signature@1.0.6 │ ├─┬ debug@2.2.0 │ │ └── ms@0.7.1 │ ├── depd@1.1.0 │ ├── encodeurl@1.0.1 │ ├── escape-html@1.0.3 │ ├── etag@1.7.0 │ ├─┬ finalhandler@0.5.0 │ │ ├── statuses@1.3.1 │ │ └── unpipe@1.0.0 │ ├── fresh@0.3.0 │ ├── merge-descriptors@1.0.1 │ ├── methods@1.1.2 │ ├─┬ on-finished@2.3.0 │ │ └── ee-first@1.1.1 │ ├── parseurl@1.3.1 │ ├── path-to-regexp@0.1.7 │ ├─┬ proxy-addr@1.1.2 │ │ ├── forwarded@0.1.0 │ │ └── ipaddr.js@1.1.1 │ ├── qs@6.2.0 │ ├── range-parser@1.2.0 │ ├─┬ send@0.14.1 │ │ ├── destroy@1.0.4 │ │ ├─┬ http-errors@1.5.1 │ │ │ └── setprototypeof@1.0.2 │ │ └── mime@1.3.4 │ ├── serve-static@1.11.1 │ ├─┬ type-is@1.6.14 │ │ └── media-typer@0.3.0 │ ├── utils-merge@1.0.0 │ └── vary@1.1.0 ├─┬ mysql@2.12.0 │ ├── bignumber.js@2.4.0 │ ├─┬ readable-stream@1.1.14 │ │ ├── core-util-is@1.0.2 │ │ ├── inherits@2.0.3 │ │ ├── isarray@0.0.1 │ │ └── string_decoder@0.10.31 │ └── sqlstring@2.2.0 └── raw-body@0.0.1
请教各位,我哪里有问题?
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)就能看到了