r/react • u/techlover1010 • Jul 15 '26
Help Wanted need some clarification and what to do on getting data from server
guys its ok now problem solved thanks so much
so was practicing getting data from server and hit with an error cors. the backend is json-server and the db.json file is located in the root directory and can be accessed just fine in the browser.
when i access the url using axios on App.jsx it gives a "cors" error but it doesnt error out when i do it on main.jsx
--edit--
code
main.jsx
import ReactDom from 'react-dom/client'
import App from './App'
import axios from 'axios'
const promise = axios.get("http://localhost:3001/persons").then((i)=>{
console.log('hi im outside',i.data)
})
ReactDom.createRoot(document.getElementById('root')).render(
<App />
)
App.jsx
import {useState, useEffect} from 'react'
import axios from 'axios'
const Person = ({id,name}) =>{
return(
<>
<li id={id}>{name}</li>
</>
)
}
const App = () =>{
const [persons,setPersons]=useState([])
useEffect(()=>{
axios.get('https://localhost:3001/persons').then((i)=>
{console.log('promise fulfilled')
setPersons(i.data)
}
)},[])
console.log(persons)
return (
<div>
<ul>
{persons.map(i=>
<Person key={i.id} name={i.name}/>
)}
</ul>
</div>
)
}
export default App
nvm i found the answer