NodeJs获取不到POST参数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NodeJs获取不到POST参数相关的知识,希望对你有一定的参考价值。
NodeJs报错,从网页端获取不到POST参数,提示错误类似如下
TypeError: Cannot read property ‘username‘ of undefined
at C:\Users\RENYUZHUO\Desktop\_\Code\Server_NodeJs\login.js:13:29
at Layer.handle [as handle_request] (C:\Users\RENYUZHUO\Desktop\_\Code\Server_NodeJs\node_modules\express\lib\router\layer.js:82:5)
at next (C:\Users\RENYUZHUO\Desktop\_\Code\Server_NodeJs\node_modules\express\lib\router\route.js:100:13)
at Route.dispatch (C:\Users\RENYUZHUO\Desktop\_\Code\Server_NodeJs\node_modules\express\lib\router\route.js:81:3)
at Layer.handle [as handle_request] (C:\Users\RENYUZHUO\Desktop\_\Code\Server_NodeJs\node_modules\express\lib\router\layer.js:82:5)
at C:\Users\RENYUZHUO\Desktop\_\Code\Server_NodeJs\node_modules\express\lib\router\index.js:233:24
at Function.proto.process_params (C:\Users\RENYUZHUO\Desktop\_\Code\Server_NodeJs\node_modules\express\lib\router\index.js:311:12)
at C:\Users\RENYUZHUO\Desktop\_\Code\Server_NodeJs\node_modules\express\lib\router\index.js:227:12
at Function.match_layer (C:\Users\RENYUZHUO\Desktop\_\Code\Server_NodeJs\node_modules\express\lib\router\index.js:294:3)
at next (C:\Users\RENYUZHUO\Desktop\_\Code\Server_NodeJs\node_modules\express\lib\router\index.js:188:10)
原因其实是因为没有:
app.use(bodyParser.urlencoded({extended: false}))
那这句话到底是干嘛用的呢?
就是将网页端发送的请求的实体通过中间件解析出来,以便使用。
参见:https://github.com/expressjs/body-parser
以上是关于NodeJs获取不到POST参数的主要内容,如果未能解决你的问题,请参考以下文章
Nodejs + express post get 参数获取小结
nodejs 做http服务器的时候如何获取post过来的参数