Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java script fetch returns 200 but no data

I have a requirement to display all the countries in the world in a drop down. So I found this api end point END POINT LINK. When I copy and paste this end point link in my web browser I got a response with all the data. (countries);

When I try to embed this in project.

getCountries() {
      try {
       
        fetch(`https://restcountries.eu/rest/v1/all`).then(data =>
          console.log(data)
        );
        
      } catch (error) {
        console.log("HERE ERROR COMES", error);
      }
    }

It does go to then block of the fetch method. But gives me the output enter image description here

There is nothing called data here. Even I get a success respond. Why could this happen? Is this something related to cors errors?

like image 721
Pathum Kalhan Avatar asked Sep 15 '25 19:09

Pathum Kalhan


1 Answers

You can use as follow:

let url = 'https://restcountries.eu/rest/v1/all';

fetch(url)
.then(res => res.json())
.then((data) => {
  console.log(data);
})
.catch(err => { throw err });
like image 187
protoproto Avatar answered Sep 18 '25 08:09

protoproto