【问题标题】:AngularJS bad POST request with Express / Passport使用 Express / Passport 的 AngularJS 错误 POST 请求
【发布时间】:2017-05-05 07:56:52
【问题描述】:

尝试通过 AngularJS 使用护照进行身份验证。我发现很多线程说问题是请求的默认内容类型。尝试了几种方法,我总是收到 400 错误(错误请求)。

$http.post('/login', {
  username: $scope.user.username,
  password: $scope.user.password
})
.success(function(user) {
  console.log('success');
  // $location.url('/');
})
.error(function() {
  console.log('failed');
  // location.url('/login')
})

[剪掉一些我认为不是问题的东西]

UDPATE:我认为这可能是护照的问题。使用 body-parser 中间件,我可以知道我正在以以下格式发送数据: { 用户名:我,密码:12345 } 唉,我仍然得到错误代码 400。

var bodyParser       = require('body-parser');
var jsonParser       = bodyParser.json();
app.post('/login', jsonParser, function(req,res) {
  if (!req.body) {
    console.log('no body');
    return res.sendStatus(400);
  }

  console.log(req.body);
  res.end(req.user);
});

这是本地策略

passport.use(new LocalStrategy(
  function(username, password, done) {
    if (username === "me" && password === "123") {
      return done(null, {name: "me"});
    }
    return done(null, false, {message: 'Incorrect username or password.'});
  }
));

还有当我试图拿到护照上班时的路线。

app.post('/login',passport.authenticate('local'), function(req,res) {
  console.log('login in server.js')
  res.send(req.user);
});

根据我所阅读的所有内容,可以肯定这都是标准的。

【问题讨论】:

    标签: angularjs express post passport.js


    【解决方案1】:

    需要这个...

    var bodyParser = require('body-parser');
    app.use(bodyParser.json());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-11
      • 2016-04-11
      • 2020-05-09
      • 2017-06-29
      • 1970-01-01
      • 2019-07-23
      相关资源
      最近更新 更多