简体   繁体   English

表达http正文成为key:value-pair的关键

[英]express http body becoming key in key:value-pair

I'm having some trouble with my Angular 2 http.post-request. 我的Angular 2 http.post-request遇到了一些麻烦。

let body = JSON.stringify({user:'username', passw:'mypass'});
let headers = new Headers();

headers.append('Content-Type', 'application/x-www-form-urlencoded');

this.http.post(`http://localhost:8090/api2/drawing/12345`, body, {headers: headers}) //TODO: remove localhost
  .subscribe((res: Response)=>{
    this.data = res.json();
    console.log(this.data);
  })

In my express-app, with bodyparser, the request body looks like this: 在我的express-app中,使用bodyparser,请求主体如下所示:

{ '{"user":"username","passw":"mypass"}': '' }

when I would expect it to look more like this: 当我希望它看起来像这样:

{ user:"username", passw:"mypass" }

I have configured bodyParser like so: 我已经像这样配置了bodyParser:

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

Where have I gone wrong? 我哪里出问题了?

Thanks! 谢谢!

You're sending a JSON-encoded body with a mismatched Content-Type of application/x-www-form-urlencoded . 您正在发送的JSON编码主体的Content-Type of application/x-www-form-urlencoded不匹配。 If you want to keep the JSON body, change this: 如果要保留JSON正文,请更改以下内容:

headers.append('Content-Type', 'application/x-www-form-urlencoded');

to this: 对此:

headers.append('Content-Type', 'application/json');

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

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