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)

6
  • Possible duplicate of How do I return the response from an asynchronous call? Commented Sep 21, 2016 at 8: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. Commented Sep 21, 2016 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. Commented Sep 22, 2016 at 7: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. Commented Sep 22, 2016 at 8:08
  • meta.stackexchange.com/questions/66377/what-is-the-xy-problem Commented Sep 22, 2016 at 8:52

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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);
        }
    };
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.