我的 Angular 2 http.post 请求遇到了一些问题。
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 中,使用正文解析器,请求正文如下所示:
{ '{"user":"username","passw":"mypass"}': '' }
当我期望它看起来更像这样时:
{ user:"username", passw:"mypass" }
我已经像这样配置了身体解析器:
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
我哪里做错了?
谢谢!
您正在发送一个 JSON 编码的正文,其中包含不匹配的 application/x-www-form-urlencoded
Content-Type
。如果要保留 JSON 正文,请更改以下内容:
headers.append('Content-Type', 'application/x-www-form-urlencoded');
对此:
headers.append('Content-Type', 'application/json');