我无法在服务器上获取帖子参数。我将 Angular 2 应用程序中的 post 请求发送到 Nodejs Express 服务器。
这是我在 Angular 2 中的代码:
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();
}
}
在浏览器中,我看到帖子参数已发送,例如在 chrome 部分“请求播放负载”中包含我的帖子数据。
这是我的服务器:
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 服务进行后查询时,我的服务器工作正常。
请帮我!
您忘记导入Headers
class:
import { Injectable } from 'angular2/core';
import { Http, Headers } from 'angular2/http'; // <----
在这种情况下,标头不会与您的请求一起发送,但不会显示错误。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)