点击登录就提示这个错误,但找不到原因
TypeError: **Cannot read property 'username' of undefined**
login.js
前台js:获取输入的username password 拼成一个数组传给后台,
走同样的路,发现不同的人生
First: You should make sure ajax method is same as routers' methodSecond: 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数据拿不到.可能是header是application/x-www-form-urlencoded,而datatype里是JSON导致的.
body
header
application/x-www-form-urlencoded
datatype
JSON
后台那边如果是要获取JSON数据的话,需要保证express的中间件有使用
express
app.use(bodyParser.json());
楼主如何解决的?我遇到了同样的问题
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.
body
数据拿不到.可能是
header
是application/x-www-form-urlencoded
,而datatype
里是JSON
导致的.后台那边如果是要获取
JSON
数据的话,需要保证express
的中间件有使用楼主如何解决的?我遇到了同样的问题
TypeError: Cannot read property 'name' of undefined