Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Fetch api [object object]

Tags:

javascript

Why is the code I try to run below return as [object object]?

var request = new Request('data/some.json');

  fetch(request).then(function(response) {
    return response.json();
  }).then(function(json) {
      document.getElementById("test").innerHTML = json.items;
  });
like image 356
Emre Gozel Avatar asked Apr 29 '26 07:04

Emre Gozel


1 Answers

document.getElementById("test").innerHTML = json.items; is the issue here.

You should do:

document.getElementById("test").innerHTML = JSON.stringify(json.items);

This is because if you try to paint a plain JavaScript object in the DOM, it'll call the object's toString function, which will result in [object object].

like image 109
Josh Beam Avatar answered Apr 30 '26 19:04

Josh Beam