찾다

 >  Q&A  >  본문

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

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

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

login.js

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

怪我咯怪我咯2875일 전1228

모든 응답(4)나는 대답할 것이다

  • 巴扎黑

    巴扎黑2017-04-17 14:45:25

    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
    }));

    회신하다
    0
  • ringa_lee

    ringa_lee2017-04-17 14:45:25

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

    회신하다
    0
  • 怪我咯

    怪我咯2017-04-17 14:45:25

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

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

    app.use(bodyParser.json());

    회신하다
    0
  • 阿神

    阿神2017-04-17 14:45:25

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

    TypeError: Cannot read property 'name' of undefined

    회신하다
    0
  • 취소회신하다