JS Fetch: Get JSON data from server


/ Published in: JavaScript

Get JSON data from server via Fetch API


Copy this code and paste it in your HTML
  1. fetch('products.json') // some API path
  2. // as response comes in form of string we use response.json to parse it in JSON
  3. .then(function(response) { return response.json(); })
  4. // parse JSON in form of parameter innext line
  5. .then(function(json) {
  6. for(var i = 0; i < json.products.length; i++) {
  7. console.log(json.products[i])
  8. }
  9. });

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.