I am trying to get data from the News API on my front end application. I am using async-await with the fetch API, but I keep getting an error of:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://newsapi.org/v2/everything?q=covid&apiKey=d69affb88e344a3da1b2a12da50f680c. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
When I try to console log each news title, that's when I get an error.
// async await newsApi fn
async function getNews() {
const response = await fetch(news_api_url)
if (response.status === 200) {
console.log('Success , NEWS loaded')
const data = await response.json()
console.log(data);
return data
} else {
throw new Error(' Unable to fetch News API data')
}
}
getNews()
.then(data => {
data['articles'].forEach(article => {
let newsTitle = document.createElement('div')
let link = document.createElement('a')
newsTitle.classList.add('ticker__item')
link.appendChild(newsTitle)
link.href = article.url
newsTitle.textContent = article.title
newsTickerEl.appendChild(link)
titles.push(article.title)
console.log(newsTitle);
localStorage.setItem("titles", JSON.stringify(titles));
});
})