Discussion about CORS errors when making HTTP/HTTPS requests in a Sanity.io app.
<!DOCTYPE html>
<html>
<head>
<title>My Pets</title>
<meta charset="UTF-8" />
<link rel="stylesheet" href="src/styles.css" />
</head>
<body>
<header>
<h1>Sanity + Vanilla JavaScript</h1>
</header>
<main>
<h2>Pets</h2>
<ul>
<li>Loading pets…</li>
</ul>
<div>
<pre>
¯\_(ツ)_/¯
Your data will show up here when you've configured everything correctly
</pre>
</div>
</main>
<script>
let DATASET = "production";
let PROJECTID = "zk1lyy0l";
let QUERY = encodeURIComponent("*[_type == 'pet'");
let url = `https://${PROJECTID}.<http://api.sanity.io/v2021-10-21/data/query/${DATASET}?query=${QUERY}`;|api.sanity.io/v2021-10-21/data/query/${DATASET}?query=${QUERY}`;>
fetch(url)
.then((res) => res.json())
.then(({ result }) => {
console.log(result);
let petList = document.querySelector("ul");
let firstItem = petList.querySelector("li");
if (result.length > 0) {
petList.removeChild(firstItem);
result.forEach((pet) => {
petList.appendChild(
(document.createElement("li").textContent = pet?.name)
);
});
let pre = (document.querySelector(
"pre"
).textContent = JSON.stringify(result, null, 2));
}
})
.catch((err) => console.error(err));
</script>
</body>
</html><https://codesandbox.io/s/get-started-with-sanity-vanilla-javascript-starter-forked-khv63f?file=/index.html>This is the error I'm getting: Access to fetch at '<https://vortex.data.microsoft.com/collect/v1>' from origin '<https://codesandbox.io>' has been blocked by CORS policy: Request header field content-encoding is not allowed by Access-Control-Allow-Headers in preflight response.<http://zk1lyy0l.api.sanity.io/v2021-10-21/data/query/production?query=*%5B_type%20%3D%3D%20%27pet%27:1|zk1lyy0l.api.sanity.io/v2021-10-21/data/query/production?query=*%5B_type%20%3D%3D%20%27pet%27:1> Failed to load resource: the server responded with a status of 400 ()<http://zk1lyy0l.api.sanity.io/v2021-10-21/data/query/production?query=*%5B_type%20%3D%3D%20%27pet%27:1|zk1lyy0l.api.sanity.io/v2021-10-21/data/query/production?query=*%5B_type%20%3D%3D%20%27pet%27:1> Failed to load resource: the server responded with a status of 400 ()]at the end).
Was this answer helpful?
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.