Skip to content
Advertisement

Fetch data is shown in console.log, but not in HTML TAG

I’m trying to fetch a JSON data from our server and then inserting it in a <script> TAG in client-side. See below:

JavaScript

The problem is: when I console.log(data.imports), it shows me the fetched data, but the <script> TAG looks like this:

JavaScript

What it looks strange for me is when I use JSON.stringify(data), it is possible to see that the data was extracted from the API as expected (see below), but in JSON format it doesn’t extract anything…

JavaScript

Could anyone tell me where I’m doing wrong?

Advertisement

Answer

Anything you assign to innerHTML will be implicitly converted to a string. Hence, the [object Object] you’re seeing. In order to see the actual JSON value, you can explicitly convert this object to a JSON-string:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement