Cody Raspien Cody Raspien - 1 year ago 134
JSON Question

FETCH API returning [object Object]

I am using FETCH API to get a value stored in a json file. That value has to go into a variable.

The problem is - the variable ends up holding [object Object] as value.

var title = fetch('URL/out.json', {mode: 'cors'});

In the htaccess of the server hosting the json file, there is the line

Header set Access-Control-Allow-Origin "*"

The json is as follows


I think the json might be the culprit.

I cannot find the reason for [object Object] to be the variable value.

Can I use Fetch to get a hosted text file? I tried - couldn't get it work. - just thinking of an alternative.

Answer Source

Try using .then() as described here:

fetch('URL/out.json', {mode: 'cors'}).then(function(response) {
    return response.blob();
}).then(function(response) {
    // process response
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download