5
我无法在服务器上获取后参数。我将Angular 2应用程序中的发布请求发送给Nodejs express服务器。 这里我在角2码:Angular 2 http post + Nodejs express
import { Injectable } from 'angular2/core';
import { Http } from 'angular2/http';
@Injectable()
export class QueryService {
static get parameters() {
return [[Http]]
}
constructor(http) {
this.http = http;
}
postApi() {
var headers = new Headers();
headers.append('Content-Type', 'application/json');
return this.http.post('http://localhost:3001/post_test', JSON.stringify({"id": 1, "name": "2"}), { headers: headers }).toPromise();
}
}
在浏览器中我看到post数据被发送,例如铬段“请求Playload”包含我的帖子的数据。 在这里,我的服务器:
app.js:
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
路线/ index.js:
exports.post_test = function(req, res) {
console.log('post_test ', req.body);
}
,输出是 “post_test {}”
我可以”不明白,问题在哪里。因为我的服务器工作正常,当我使用Angular 1 $ http服务进行后期查询时。 请帮帮我!