Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

 {
   lollol
}

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.

like image 654
Cody Raspien Avatar asked Jan 18 '26 02:01

Cody Raspien


2 Answers

Try using .then() as described here:

fetch('URL/out.json', {mode: 'cors'}).then(function(response) {
    return response.blob();
}).then(function(response) {
    // process response
});
like image 167
Justinas Avatar answered Jan 19 '26 16:01

Justinas


you need to stringify the object to convert it into JSON string.

try JSON.stringify(theObject)

like image 34
iJade Avatar answered Jan 19 '26 14:01

iJade



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!