Home > Blockchain >  Promise Rejection with axios. try to make HTTP instead of HTTPs [ duplicate ]
Promise Rejection with axios. try to make HTTP instead of HTTPs [ duplicate ]

Time:12-19

I pull data from the API and write it to the application with usestate() when the app runs there are no problems, but after 10-30 seconds I get this error.

Error Image

Here is my code.

const App = () => {
 const [datas, setDatas] = useState([])

 const res = async () => {
  const response = await axios.get("http://hasanadiguzel.com.tr/api/kurgetir")
  setDatas(response.data.TCMB_AnlikKurBilgileri)
 }

 res()

 return (
  <SafeAreaView style={style.container}>
   <View>
    {datas.map((item) => {
     return (
      <KurCard
       title={item.Isim}
       alis={item.BanknoteBuying}
       satis={item.BanknoteSelling}
      />
     )
    })}
   </View>
  </SafeAreaView>
 )
}

How can I fix this ?

CodePudding user response:

Hi @n00b,

The problem is with your URL Protocol.

const App = () => {
  const [datas, setDatas] = useState([]);

  const res = async () => {
    try {
      const url = "https://hasanadiguzel.com.tr/api/kurgetir";
      const response = await axios.get(url);
      const data = await response.data;
      console.log(data.TCMB_AnlikKurBilgileri); // check you console.
      setDatas(response.data.TCMB_AnlikKurBilgileri);
    } catch (error) {
      console.log(error);
    }
  };

  useEffect(() => {
    res();
  }, []);

And also check this out:- Codesandbox.

And please read this Stack Overflow discussion for better understanding:- stackoverflow

  • Related