3

I am creating a website that reads externally hosted json files and then uses node.js to populate the sites content.

Just to demonstrate what I'm after, this is a really simplified version of what I'm trying to do in node.js

var ids = [111, 222, 333];

ids.forEach(function(id){
    var json = getJSONsomehow('http://www.website.com/'+id+'.json');

    buildPageContent(json);
});

Is what I want to do possible?

(Marked as a duplicate of "How do I return the response from an asynchronous call?" see my comment below for my rebuttal)

Community
  • 1
  • 1
Daniel Tonon
  • 9,261
  • 5
  • 61
  • 64
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Ben Fortune Sep 21 '16 at 08:46
  • I don't believe it is a duplicate since this question is specifically centred around node.js and that question is centred around browser based javascript. node.js is a very different platform to browser based JS since node.js manipulates files on your system and browser js manipulates the DOM of a website. Also node.js has access to the gazzillion npm plugins out there so there is probably a better solution using an npm plugin rather than using ajax since ajax is mainly designed for browser based javascript. Also npm does not natively support XMLHttpRequest whereas browser based js does. – Daniel Tonon Sep 21 '16 at 23:21
  • No, it's more or less the same. The only main difference is the APIs. The concept and syntax of asynchronous JavaScript is exactly the same. Be it callbacks, generators or promises. If you had actually read the duplicate you'd understand that. – Ben Fortune Sep 22 '16 at 07:16
  • The question isn't asking about how to do asynchronous js though. It's primarily asking about how to access content from an external source using node.js. – Daniel Tonon Sep 22 '16 at 08:08
  • http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – Ben Fortune Sep 22 '16 at 08:52
  • I've edited the question and removed my attempted solution. That fixes the potential XY aspect of the question. I was primarily concerned with the getJSONsomehow() part of the question. Happy now? – Daniel Tonon Sep 22 '16 at 23:43

2 Answers2

4

You are trying to get it synchronously. What you should aim for instead, is not a function used like this:

var json = getJSONsomehow('http://www.website.com/'+id+'.json');

but more like this:

getJSONsomehow('http://www.website.com/'+id+'.json', function (err, json) {
  if (err) {
    // error
  } else {
    // your json can be used here
  }
});

or like this:

getJSONsomehow('http://www.website.com/'+id+'.json')
  .then(function (json) {
    // you can use your json here
  })
  .catch(function (err) {
    // error
  });

You can use the request module to get your data with something like this:

var request = require('request');
var url = 'http://www.website.com/'+id+'.json';

request.get({url: url, json: true}, (err, res, data) => {
  if (err) {
    // handle error
  } else if (res.statusCode === 200) {
    // you can use data here - already parsed as json
  } else {
    // response other than 200 OK
  }
});

For a working example see this answer.

For more info see: https://www.npmjs.com/package/request

Community
  • 1
  • 1
rsp
  • 107,747
  • 29
  • 201
  • 177
0

I think problem is in async request. Function will return result before request finished.

AJAX_req.open( "GET", url, true );

Third parameter specified async request.

You should add handler and do all you want after request finished.

For example:

function AJAX_JSON_Req( url ) {
    var AJAX_req = new XMLHttpRequest.XMLHttpRequest();
    AJAX_req.open( "GET", url, true );
    AJAX_req.setRequestHeader("Content-type", "application/json");

    AJAX_req.onreadystatechange = function() {
        if (AJAX_req.readyState == 4 && AJAX_req.status == 200) {
            console.log(AJAX_req.responseText);
        }
    };
}
Sabik
  • 1,599
  • 1
  • 11
  • 10