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

234
Visualizações
JavaScript code runs in node.js but not browser

I have this code:

async function my_function() {
    console.log("Hello");

    let response = await fetch(url, {
        mode: 'no-cors'
    })
        .then(response => response.json()) // THIS LINE

        .then(response => {
            console.log(response);
        })
}

which works great if I include const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args)); in node.js. This however, does not run in the browser. When I run this code in google chrome I get this error

Uncaught (in promise) SyntaxError: Unexpected end of input

relating to the

.then(response => response.json()) // THIS LINE

line. What am I doing wrong?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

For the browser, you have to enable the cors-policy otherwise it will not works. So in your API, you must include:

"Access-Control-Allow-Origin" : "*", 

And also disable the no-cors mode.

about 4 years ago · Juan Pablo Isaza Relatório

0

Because You mixing Promises with await? Choice one of. Read about this https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await

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