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
Add a ment  | 

2 Answers 2

Reset to default 6

This 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