Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!