Stéphane R. Stéphane R. - 2 months ago 18
JSON Question

Return data from json and AJAX

I would like display all Jobs with XMLHttpRequest from json data, but i can not display my data. This my function :

function getJobs () {
let httpRequest = new XMLHttpRequest();
httpRequest.open('GET', 'http://codepen.io/jobs.json', true);
httpRequest.onreadystatechange = () => {
if (httpRequest.readyState == 4) {
let data = JSON.parse(httpRequest.responseText);
Object.keys(data).forEach( (key) => {
console.log(key[0].company_name); // undefine
});
}
};
httpRequest.send();
}

getJobs();


Can you help me about my forEach ? The json is object of array, with some object ^^

Thank you !

Answer

Try this

function getJobs () {
  let httpRequest = new XMLHttpRequest();
  httpRequest.open('GET', 'http://codepen.io/jobs.json', true);
  httpRequest.onreadystatechange = () => {
    if (httpRequest.readyState == 4) {
      let data = JSON.parse(httpRequest.responseText);
      data.jobs.forEach(function(key, value) {
         console.log(key.company_name);
      });
    }
  };
 httpRequest.send();
}
Comments