ChatGPT gave me a fetch example that works locally but fails with a CORS error online
hi everyone, im making a little game site with a high score table. i asked chatgpt for the code to load the scores and it gave me this fetch:
Cite
CoursesWeb Forum, "ChatGPT gave me a fetch example that works locally but fails with a CORS error online", https://coursesweb.net/forum/t/chatgpt-gave-me-a-fetch-example-that-works-locally-but-fails/, accessed 2026-11-29.fetch("https://api.mygame.example.com/scores") .then((res) => res.json()) .then((data) => console.log(data));on my laptop it works fine (i run the page and the node server together on localhost:3000). but after i uploaded the page to my hosting it just breaks and the console says:
Access to fetch at 'https://api.mygame.example.com/scores' from origin 'https://mygame.example.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.i asked chatgpt again and it told me to add mode: "no-cors" to the fetch. the error goes away but then res.json() fails and i get nothing. i dont really get what CORS even is, only that it hates me. the server is a small Express app. what do i change?
Write something first.
Give people something to work with: at least 30 words on what happened and what you tried.
That is too long. Keep it under 6,000 characters.
Write the question as the title, 15 to 140 characters, no links.
Pick a category.
Add a name (2 to 40 characters, no links).
That email address does not look right.
That was quick. Read the thread, then try again.
The form expired. Reload the page and post again.
Something went wrong with the form. Reload and try again.
Please complete the check and post again.
Limit reached for now. Try again later.
This thread is closed to new replies.
This thread no longer accepts replies.
Something went wrong with the form. Reload and try again.
Post a reply or question first (name and email), then this browser can vote, edit and accept answers.
You cannot vote on your own post.
Only the author (within 30 days) or the forum team can do that.
That email already has an account. Use your sign-in link instead.
New accounts are paused for the moment. Try again later.
That was already posted.