Access to XMLHttpRequest at from origin blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present

Issue

This Content is from Stack Overflow. Question asked by Channel Supporter

I was adjusting my game locally but suddenly I were getting CORS errors.
So I switched to Github where I had a working copy without any changes/adjustments but somehow even that wasn’t working anymore.

My site: https://snake1v1.netlify.app/

When I check the Console log it shows me this:

Access to XMLHttpRequest at 'https://sleepy-island-33889.herokuapp.com/socket.io/?EIO=3&transport=polling&t=ODHUI9f' from origin 'https://snake1v1.netlify.app/' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
index.js:83

GET https://sleepy-island-33889.herokuapp.com/socket.io/?EIO=3&transport=polling&t=ODHUI9f net::ERR_FAILED 503

I’ve been using socket io V.2.3 and had to enable CORS manually via this code:

const http = require("http");
const socket_io = require("socket.io");

const httpServer = http.createServer();
const io = new socket_io.Server(httpServer, {
  cors: {
    origin: "https://snake1v1.netlify.app",
//        origin: "http://127.0.0.1:8080",
//        origin: "https://sleepy-island-33889.herokuapp.com/",
//        origin: "https://tranquil-refuge-03880.herokuapp.com/",
        methods: ["GET", "POST"]
  },
})();

I tried with different links but none of them work…

I also found this code snippet:
Access-Control-Allow-Origin:

I have two questions though, do I have to replace the with my netifly or heroku domain?
And where do I have to put in this code? In my server.json or index.html?



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?