using free national API to get data, "blocked by CORS policy" but works with postman/regular url

Viewed 187

I am trying to get books list from National Library API (free for public use). It gives back JSON when visiting the URL in Chrome or hitting the endpoint with Postman. However, I can't get rid of the error when trying to make it through localhost, while developing. when I add: { mode: "no-cors"} to the fetch options, I get a response back, but opaque type, so I can't read the data.

fetch(
  `https://api.nli.org.il/openlibrary/search?api_key=uoX9ZMiaRMIBUF4pX0SEuGXMRpUqQIHrw5XuQKcE&query=title,exact,,AND;language,exact,heb&output_format=json&material_type=books`,
  {
    method: "GET",
    headers: {
      "Access-Control-Allow-Origin": "*",
    },
  }
).then((response) => console.log(response));

Error in console:

Access to fetch at 'https://api.nli.org.il/openlibrary/search?api_key=uoX9ZMiaRMIBUF4pX0SEuGXMRpUqQIHrw5XuQKcE&query=title,exact,%D7%97%D7%A0%D7%95%D7%9A,AND;language,exact,heb&output_format=json&material_type=books' from origin 'http://127.0.0.1:5500' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

0 Answers
Related