javascriptjsonfile

how to get file content in javascript?


I used to get file content using JQuery this way:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$.get('/data.json', function(data) {
console.log(data);
});
</script>

now I need to do it just using javascript, would you please help me :)


Solution

  • Use the fetch api.

    <script>
       fetch('/data.json')
          .then(res => res.json())
          .then(data => console.log(data))
    </script>