0

I am still new svelte JS. I am trying to fetch data but getting this error.

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.allnigerianewspapers.com.ng/api/news. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing). Status code: 301.

Please, I need a solution.

<script>
    import { onMount } from "svelte";
    const endpoint = "https://api.allnigerianewspapers.com.ng/api/news";
    let posts = [];
  
    onMount(async function () {
      const response = await fetch(endpoint);
      const data = await response.json();
      console.log(data);
    });
  
   
  </script>
  
  <main>
    
  </main>
  
  <style>
    /* ommitted for brevity */
  </style>
  • 2
    Does this answer your question? [Sveltekit proxy api to avoid cors](https://stackoverflow.com/questions/70472978/sveltekit-proxy-api-to-avoid-cors) – Mehdi Mar 07 '22 at 00:38
  • I don't know why is not working, though the endpoint needs pagination. I would prefer if I can just see sample code for my answer – Olaniyan Adewale Mar 07 '22 at 10:25
  • The reason you can't access the API from your own custom-built frontend is that the original publisher set up CORS precisely to prevent you (and others) from doing so. The only way around that is to set up your own API as a proxy to the original API, and query your proxy API instead. The way to do that is explained in the link @Mehdi kindly provided you. – Thomas Hennes Mar 08 '22 at 00:08

0 Answers0