Skip to content
CoursesWeb Forum

ChatGPT gave me a fetch example that works locally but fails with a CORS error online

Asked by Zaid M. · AI Coding Assistants and MCP2 repliesupdated

Short answer

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:

Written by Zaid M. ·
CiteCoursesWeb 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.
  1. Member ·
    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?

    0
    ReplyLink

Plain text or simple Markdown. Links are nofollow. Your email is never shown.

  1. Bertie H.Member ·

    Open the Network tab and look at the response headers of that request. If there is no Access-Control-Allow-Origin in the list, the server is the problem, not your fetch. Use the browser's own view before you ask a chatbot.

  2. Jun P.Member ·

    There is an npm package called cors that does this for Express. npm install cors, then app.use(cors()) before your routes. It works, but check the options, because with no arguments it allows every origin.

CoursesWeb Forum. Questions and answers on learning web development: HTML, CSS, JavaScript, PHP, MySQL, Node.js and AI coding help.