0

I am trying to take this api: https://newsapi.org/v1/articles?source=techcrunch&sortBy=top&apiKey=9a9c9037fecf4bcb9e49ffa581f8713b and assign it to a variable in JS where if i console.log(variable) inside of a console of a browser it will give me a tree pertaining to the api. This is probably a horrible explanation, so here's a picture of what i'm looking to get: https://i.stack.imgur.com/bjipu.jpg

Barren Code: https://i.stack.imgur.com/IfVGZ.jpg src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js' in HTML I used this Yahoo YQL in previous RSS apps I've made but clearly it doesn't work the same on this one

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • I assume you are getting a stringified response, so you just need to parse it to a variable `var response = JSON.parse(response);` – markoffden Jun 12 '17 at 20:43
  • Not sure if i stick the whole url in the JSON.parse() but i get Uncaught SyntaxError: Unexpected token h in JSON at position 0 at JSON.parse () at js.js:3 when i do – Tomer Bonilla Jun 12 '17 at 20:45
  • have you tried to search for it? like Ajax / fetch / call an api from javascript? – webdeb Jun 12 '17 at 20:46
  • Oh, so you didn't even call it yet. Post some code pls – markoffden Jun 12 '17 at 20:46
  • It's valid json. `var response = JSON.parse(response);` should work, unless the ajax call explicitly declares to return json in which case the response variables should already be json. Show some code and some `console.log()`. – Goose Jun 12 '17 at 20:47
  • What you want to look into is how to do an ajax GET request from javascript. References: https://stackoverflow.com/questions/8567114/how-to-make-an-ajax-call-without-jquery https://stackoverflow.com/questions/247483/http-get-request-in-javascript – Jako Basson Jun 12 '17 at 20:48

1 Answers1

0
var variable;
function reqListener () {
  variable=oReq.response;
  console.log(variable);
}

var oReq = new XMLHttpRequest();
oReq.addEventListener("load", reqListener);
oReq.responseType="json";
oReq.open("GET", "https://newsapi.org/v1/articles?source=techcrunch&sortBy=top&apiKey=9a9c9037fecf4bcb9e49ffa581f8713b");
oReq.send();