node.js 报错 Cannot read property 'username' of undefined

点击登录就提示这个错误,但找不到原因

 TypeError: **Cannot read property 'username' of undefined**

clipboard.png

login.js

clipboard.png

前台js:获取输入的username password 拼成一个数组传给后台,

clipboard.png

阅读 13.3k
5 个回答

First: You should make sure ajax method is same as routers' method
Second: You should make sure parse the data in body.

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: true
}));

contentType: "application/json;charset=utf-8"

body数据拿不到.
可能是headerapplication/x-www-form-urlencoded,而datatype里是JSON导致的.

后台那边如果是要获取JSON数据的话,需要保证express的中间件有使用

app.use(bodyParser.json());

楼主如何解决的?我遇到了同样的问题

TypeError: Cannot read property 'name' of undefined

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题