goz*_*lli 8 api json http-post meteor iron-router
我有一个Iron-router路由,我希望通过HTTP POST请求接收lat/lng数据.
这是我的尝试:
Router.map(function () {
this.route('serverFile', {
path: '/receive/',
where: 'server',
action: function () {
var filename = this.params.filename;
resp = {'lat' : this.params.lat,
'lon' : this.params.lon};
this.response.writeHead(200, {'Content-Type': 'application/json; charset=utf-8'});
this.response.end(JSON.stringify(resp));
}
});
});
Run Code Online (Sandbox Code Playgroud)
但是查询服务器:
curl --data "lat=12&lon=14" http://127.0.0.1:3000/receive
Run Code Online (Sandbox Code Playgroud)
退货{}.
也许params不包含发布数据?我试图检查对象和请求,但我找不到它.
Chr*_*itz 14
iron-router中的connect框架使用bodyParser中间件来解析正文中发送的数据.bodyParser使request.body对象中的数据可用.
以下适用于我:
Router.map(function () {
this.route('serverFile', {
path: '/receive/',
where: 'server',
action: function () {
var filename = this.params.filename;
resp = {'lat' : this.request.body.lat,
'lon' : this.request.body.lon};
this.response.writeHead(200, {'Content-Type':
'application/json; charset=utf-8'});
this.response.end(JSON.stringify(resp));
}
});
});
Run Code Online (Sandbox Code Playgroud)
这给了我:
> curl --data "lat=12&lon=14" http://127.0.0.1:3000/receive
{"lat":"12","lon":"14"}
Run Code Online (Sandbox Code Playgroud)
另见:http: //www.senchalabs.org/connect/bodyParser.html
| 归档时间: |
|
| 查看次数: |
6748 次 |
| 最近记录: |