简体   繁体   English

MEAN 400错误请求

[英]400 Bad Request on MEAN

so I'm posting this question after a hours of research .... That's the answer of the browser and I can't find any reason why... So, after the login, I want to fetch the score of the user logged. 所以我经过一个小时的研究后才发布了这个问题...。这是浏览器的答案,我找不到任何原因...因此,登录后,我想获取登录用户的分数。 The score is located in a subtable of the user table (Mongo ...). 分数位于用户表的子表中(Mongo ...)。 But the call for the server doesn't work. 但是对服务器的呼叫无法正常工作。 Here is my code : 这是我的代码:

login.component.ts login.component.ts

this.dbService.scoresDownload(this.username).subscribe(scoresDownload => {
                console.log(scoresDownload);
              });

db.service.ts db.service.ts

  scoresDownload(username) {
this.createAuthenticationHeaders();
console.log(username)
return this.http.post(this.domain + 'training/scoresDownload', username, this.options).map(res => res.json());

} }

training.js (route in Express) training.js(在Express中路由)

          router.post('/scoresDownload', (req, res) => {

// let wordScoresDb = [];
console.log(req)

User.find({}, (err, result) => {
  if (err) {
    res.json({ success: false, message: err }); // Return connection error
  } else {
    res.json({ success: true, message: result }); // Return as vailable username
  }
})});

It works well if I use a get instead of a post .... And the most tricky part is that it works with all my other calls. 如果我使用get而不是post ...,它会很好用。最棘手的部分是,它可以与其他所有调用一起使用。 So, I really don't know where I mess up ... Cheers guys ! 所以,我真的不知道我在哪里搞砸了……干杯!

EDIT : I forgot to mention the error sent by express 编辑:我忘了提到快递发送的错误

SyntaxError: Unexpected token # in JSON at position 0
at Object.parse (native)
at createStrictSyntaxError (D:\Code\Deutsch_App_MEAN\node_modules\body-parser\lib\types\json.js:157:10)
at parse (D:\Code\Deutsch_App_MEAN\node_modules\body-parser\lib\types\json.js:83:15)
at D:\Code\Deutsch_App_MEAN\node_modules\body-parser\lib\read.js:121:18
at invokeCallback (D:\Code\Deutsch_App_MEAN\node_modules\raw-body\index.js:224:16)
at done (D:\Code\Deutsch_App_MEAN\node_modules\raw-body\index.js:213:7)
at IncomingMessage.onEnd (D:\Code\Deutsch_App_MEAN\node_modules\raw-body\index.js:273:7)
at emitNone (events.js:86:13)
at IncomingMessage.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickCallback (internal/process/next_tick.js:104:9)

好吧...我很痛苦地找到了答案...我通过MEAN传递的用户名是一个字符串...显然,来自http模块的“ post”仅接受对象或数组,而不接受字符串...Voilà:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM