0

I try to fetch a json file from remote server, I get syntax error when code is executed from local server, to avoid cors error I set mode: "no-cors"
This is my code:

async function logJSONData() {
    const response = await fetch("https://leafpark.city/data/test.json",{
    mode: "no-cors", // no-cors, *cors, same-origin
    cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
    credentials: "same-origin", // include, *same-origin, omit
    headers: {
      // "Content-Type": "application/json",
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    redirect: "follow", // manual, *follow, error
    referrerPolicy: "no-referrer" // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
  });

  const jsonData = await response.json();
  initMap(jsonData);
  return(jsonData);
}

let jsonData = logJSONData();

I get this error: dex.js:120 Uncaught (in promise) SyntaxError: Unexpected end of input
the error is in this line: const jsonData = await response.json();
Most of my code is copy-pasted from documentation examples from the Fetch API, so I really don't know what's happening.

Ferex
  • 553
  • 6
  • 22
  • `mode: "no-cors"` will just prevent you from accessing the response, is not a bypass, rtm: https://developer.mozilla.org/en-US/docs/Web/API/Request/mode – Lawrence Cherone Apr 18 '23 at 20:28

1 Answers1

0

In my experience, whenever i see " Unexpected end of input " i assume my response object is not what i think it is, for example i got this error a couple times when trying to de-serialize the response as an object but the response was an array.

It may also be that the server response is just not a valid JSON format.

root
  • 164
  • 11