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

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

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

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

login.js

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


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

TypeError: Cannot read property 'name' of undefined


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());
【热门文章】
【热门文章】