-3

How would I display data from this json file when the extension is not .json? The other related questions i've found have involved files with .json as an extension and as a bit of a beginner to this I am struggling with it.

https://fantasy.premierleague.com/drf/elements/

Thanks.

  • "display" in what context? How are you fetching the file? The extension should play no part in it. It's just a URL. – ADyson Feb 20 '17 at 15:33
  • I want to take content from the json and display it in an html file using javascript. – Liam Rawsthorne Feb 20 '17 at 15:38
  • 1
    See the answer here: http://stackoverflow.com/questions/14388452/how-do-i-load-a-json-object-from-a-file-with-ajax . Just replace "pathToFile.json" with your URL. Obviously that answer doesn't format the data for you, but it does show you how to fetch it with ajax. After that, you need to build some HTML to display it however you want, and render that to the DOM. That's very easy to google. – ADyson Feb 20 '17 at 15:42

1 Answers1

0

Just treat it as if it was a JSON file except that there wont be a .json extension to your URL. So in pure JavaScript you can do this:

var request = new XMLHttpRequest();
var url = 'https://fantasy.premierleague.com/drf/elements/';
var jsonResponse;

httpRequest.onreadystatechange = parseJSON;
request.open('GET', url);
request.send();

function parseJSON() {
    if (request.readyState() === XMLHttpRequest.done) {
         if (request.status == 200) {
             jsonResponse = JSON.parse(request.responseText);
         } else {
             console.log('An error occurred while processing the request.');
         }
    }
}
Valery Melou
  • 60
  • 2
  • 12