Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

227
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda