Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

358
Visualizações
Client-side code to fix CORs issue when using fetch (without cors module)

It searched and already included the typical backend code

app.use(function (req, res, next) {
    //res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000/storeScore');
    res.header('Access-Control-Allow-Origin', '*');
    //res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
    res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
    next();
});

but I still get Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:3000/storeScore. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing). followed by

Uncaught (in promise) TypeError: NetworkError when attempting to fetch resource. I mean don't I have the header res.header('Access-Control-Allow-Origin', '*'); ?

My frontend code is

function someFunction() {
            const sessionHighscore = cumulativeScore;
            const options = {
                method: 'POST',
                headers: {
                    //'Origin' : 'http://localhost:80',
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(sessionHighscore)
            };

            fetch('http://localhost:3000/storeScore', options);
        }

Maybe I need to include something in client-side header? I think local html is hosted on port 80?

Notes:

  • I am trying to send data from a local website to a mysql database on localhost:3000
  • I am using express for backend, and trying Not to use cors module as solution
  • Also is there any resources to show me how to execute routes without manually changing localhost? Like to use localHost:3000/createTable I have to type this manually into the url, can I do this with code?

Edit:

  • Looking at network tab in my browser I found the OPTIONS request is successful but the POST after is missing the 'Access-Control-Allow-Origin' header; I just need to know to how to ensure the POST also gets the header
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Please install package npm i cors.

then apply middleware as:

require as

 const cors = require("cors");   

middleware code

app.use(cors())
about 4 years ago · Juan Pablo Isaza Relatório

0

On your fetch request, add useCredentials: true.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda