簡體   English   中英

表達http正文成為key:value-pair的關鍵

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

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

在我的express-app中,使用bodyparser,請求主體如下所示:

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

當我希望它看起來像這樣:

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

我已經像這樣配置了bodyParser:

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

我哪里出問題了?

謝謝!

您正在發送的JSON編碼主體的Content-Type of application/x-www-form-urlencoded不匹配。 如果要保留JSON正文,請更改以下內容:

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

對此:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM