0
var paint = document.getElementsByTagName("img");

document.addEventListener("DOMContentLoaded", () => {
    let radomID = Math.floor(Math.random() * 45734 + 1);

let link = "//collectionapi.metmuseum.org/public/collection/v1/objects/" + radomID;

const options = {
    method: "GET",
    headers: {
        "Content-Type": "application/json"
    },
    'Access-Control-Allow-Origin' : 'https://collectionapi.metmuseum.org/'
    };

fetch(link, options)

    .then((response) => {
        if(response.ok) {
            return response.json();
        } else {
            throw new Error(response.status);
        }
    })

    .then((response) => {
        console.log(response.constituentWikidata_URL);
    })

    .catch((error) => {
        console.log(error);
    });
});

Hello, I´m here doing a petition and I want to know what's the next step to fix this problem:

Access to fetch at 'http://collectionapi.metmuseum.org/public/collection/v1/objects/43823' from origin 'http://127.0.0.1:5500' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request. script.js:16

GET http://collectionapi.metmuseum.org/public/collection/v1/objects/43823 net::ERR_FAILED (anónimo) @ script.js:16 script.js:31

TypeError: Failed to fetch

Maybe I have now to learn any back end technology and make the petitions from there.

The API doesn't need APIKey.

Thank you.

mtm
  • 504
  • 1
  • 7
  • 25
ButchBet
  • 11
  • 1
  • 1
    CORS is something enabled on the server. There is nothing you can do about it client-side. Also, `Access-Control-Allow-Origin` is a **response** header, it does not belong in your request. Also also, GET requests have no body so do not require a `Content-type` header – Phil Aug 06 '21 at 03:07
  • 1
    works in the demo.... – xianshenglu Aug 06 '21 at 03:09
  • That site does appear to have enabled CORS. All you have to do is remove your custom headers and it should work. Also, make sure you use the `https` URL – Phil Aug 06 '21 at 03:11

0 Answers0