admin管理员组文章数量:1302287
I have the following route:
exports.index = function(req, res){
res.render('index', { title: 'Express' });
};
I would like to call the following web service: and tell it to return JSON.
I have tried something like this in the index request but it errors:
var options = {
host: 'ergast',
port: 80,
path:'/api/f1/current/last/results.json'
};
http.get(options, function(response) {
response.setEncoding('utf-8');
console.log("Got response: " + response.statusCode);
var data = JSON.parse(response);
}).on('error', function(e) {
console.log("Got error: " + e.message);
}).on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
I'm guessing I'm probably missing the point somewhere.
Thanks
I have the following route:
exports.index = function(req, res){
res.render('index', { title: 'Express' });
};
I would like to call the following web service: http://ergast./api/f1/current/last/results and tell it to return JSON.
I have tried something like this in the index request but it errors:
var options = {
host: 'ergast.',
port: 80,
path:'/api/f1/current/last/results.json'
};
http.get(options, function(response) {
response.setEncoding('utf-8');
console.log("Got response: " + response.statusCode);
var data = JSON.parse(response);
}).on('error', function(e) {
console.log("Got error: " + e.message);
}).on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
I'm guessing I'm probably missing the point somewhere.
Thanks
Share edited Oct 3, 2012 at 21:01 Jon asked Oct 3, 2012 at 20:33 JonJon 40.1k87 gold badges242 silver badges393 bronze badges 2- API -> Response Formats -> JSONP – Andreas Commented Oct 3, 2012 at 20:38
- You're remending JSONP? I've updated my question. – Jon Commented Oct 3, 2012 at 20:44
2 Answers
Reset to default 6This should be simple :) I remend you using the request module (npm install request, or just add it to your packages.json file).
Then you can do the following:
var request = require("request");
request.get("http://ergast./api/f1/current/last/results.json", function (err, res, body) {
if (!err) {
var resultsObj = JSON.parse(body);
//Just an example of how to access properties:
console.log(resultsObj.MRData);
}
});
I see the suggestion about using JSONP instead of just going straight for the JSON API.
JSONP's reason for existing is for cross-domain APIs on the browser. Since you're running this on the server, the cross-domain restrictions are not an issue and thus JSONP is not required. Go ahead and do as you wish anyway!
EDIT: I ain't sure about why you don't try this. If it is for error management, I have updated the code with error management now.
The first argument you supplied to http.get
isn't correct. See the node.js docs regarding this function. Instead of passing in options
just pass in the full URL as a string, e.g.
http.get('http://ergast./api/f1/current/last/results', function(res) {
...
EDIT: After your edit, the options
argument still isn't correct. If you want to use an options dictionary, specify this:
{ host: 'ergast.', port: 80, path: '/api/f1/current/last/results' }
本文标签: javascriptConsume JSON web service from NodeExpress requestStack Overflow
版权声明:本文标题:javascript - Consume JSON web service from NodeExpress request? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741675717a2391870.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论