Home > Software engineering >  Not able to fetch data from api using react
Not able to fetch data from api using react

Time:09-28

i am not able to fetch my data from the api. it says it can't fetch data on the console. please help someone. The api is working on my server.

CodePudding user response:

Please give sufficient resource to answer the question. Also attach console error in question.

But i will try answering your question. It may be

  1. Try Giving in this way

const res = await fetch(apilink); const data = await res.json();

and store the data state

  1. Try axios, Its best way

    import axios from 'axios' useEffect(()=>{ axios.get(apilink).then(res=>{ setData(res.data) }) },[])

CodePudding user response:

try searching for allowed_host/crossOrigin for your backend then add your frontend url Something like this `@Controller @CrossOrigin(origins = "http://localhost:3000") public class MyController {

}`

  • Related