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

230
Visualizações
My For Each Loop doesnot work when I host

My code works in the live server of VS code but when I hosted it on Github pages it gave me and error. It seems to work fine and all of my code seems to work and I found no errors whatsoever.

link to the hosted page

link to the code

    // news parameters
const Api_Key = "...";
const category = "crypto";
const url = "https://newsapi.org/v2/top-headlines"
// https://newsapi.org/v2/top-headlines?q=crypto&apiKey=4d2ac6c2f603440a864065a48836b9f7
let newsAccordian = document.getElementById('newsAccordian')


// grab the new container
const xhr = new XMLHttpRequest()

xhr.open('GET', `${url}?q=${category}&apiKey=${Api_Key}`, true)

xhr.onload = function () {
    let json = JSON.parse(this.responseText)
    let articles = json.articles
    console.log(articles) // error is shown in this line
    let allNews = ""
    articles.forEach(function(news,index) {
        let newsContent = news.content.substring(0, news.content.length - 14);
}
xhr.send();
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Have a look at the console, you get the following error:

{"status":"error","code":"corsNotAllowed","message":"Requests from the browser are not allowed on the Developer plan, except from localhost."}

If you want to use this api outside your computer, you have to purchase licence.

about 4 years ago · Juan Pablo Isaza Relatório

0

You are getting this error because articles is not fetched completely before forEach method execute so

console.log(articles);//undefined

is undefined. Make sure your data from api is fetched completely before using forEach. You can use async await or promise to fetch data from api.

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