0

I'm trying to fetch from the wikipedia api and I get the "Uncaught (in promise) TypeError: NetworkError when attempting to fetch resource" error. I do not know if it has to do with the way I am appending params. I have no experience with that and I just googled a solution. I tested the parameters on postman so I know it's the right data.

      const searchQuery = async () => {
        const params = {
          action: "query",
          format: "json",
          list: "search",
          srsearch: "salvador dali",
        };
    
        const url = new URL("https://en.wikipedia.org/w/api.php/");
    
        for (let param in params) {
          url.searchParams.append(param, params[param]);
        }
        
        const searchData = await fetch(url);
        console.log(searchData);
        const data = await searchData.json();
        console.log(data);
      };
IT goldman
  • 14,885
  • 2
  • 14
  • 28
Hilary
  • 21
  • 6
  • Does this answer your question? [Why does my JavaScript code receive a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error, while Postman does not?](https://stackoverflow.com/questions/20035101/why-does-my-javascript-code-receive-a-no-access-control-allow-origin-header-i) – IT goldman Oct 07 '22 at 18:21

1 Answers1

0

This is a CORS issue. To get around this, make the request from the server side or use a service like CORS Anywhere.

const url = new URL("https://corsanywhere.herokuapp.com/https://en.wikipedia.org/w/api.php/"); 
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
Unmitigated
  • 76,500
  • 11
  • 62
  • 80