0
fetch('http://www.freegamesforyourwebsite.com/feeds/games/?
tag=platform&limit=100&format=json',
{
method:'GET',
mode:'no-cors',
dataType: 'json',
    headers: {
        'Accept': 'application/json'
    }  

}).then(response =>
response.json()).then(addImage).catch(e => requestError(e,'image'));

console.log

SyntaxError: Unexpected end of input
 at fetch.then.response ((index):159)
 at <anonymous>

I try using the fetch the json with cros platform, why the request header in Google developer tool shows me the provision request header.And the preview part can show me the json response, but the promise after the response promise cannot read the data and the error occurs as shown in the console log above.and the URL I fetch is the open source that can use by everyone.

any help is appreciated.

1 Answers1

3

You said mode:'no-cors', so fetch doesn't attempt to read data across origins (which is forbidden by default), and it doesn't throw an error mentioning CORS (because you told it you didn't want to try to read data across origins).

When you try to parse the body (that you can't read) as JSON, there is nothing to read so it then throws an error.

Change it to mode: "cors".

Ensure the server you are requesting the data from grants you permission to read the response using the Access-Control-Allow-Origin header.


See also this answer about the Same Origin Policy.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335