Search code examples
javascripthtmljson-api

Read data from JSON Url using plain javascript and display it in HTML


I need to read data from json generated url using plain javascript and display the data in html.i have searched alot but i couldn't find exactly what i need. Can you please give me some startup like what should i do to or any reference materials. I really want to learn and implement it on my own.

P.S: I am a beginner in JavaScript.


Solution

  • You can use ES6 fetch API to load the request data and then render it.

    fetch('http://example.com/movies.json')
      .then(function(response) {
        return response.json();
      })
      .then(function(myJson) {
        console.log(myJson);
      });
    

    Here's a tutorial on fetch API: https://scotch.io/tutorials/how-to-use-the-javascript-fetch-api-to-get-data

    Fetch is now supported in all modern browsers: https://caniuse.com/#feat=fetch