In react native application, the backend returns a simple json and I want to use it with axios on the client side, but it gives an error that I have never seen.
it works when i use a different api
useEffect(() => {
axios
.get("https://jsonplaceholder.typicode.com/users")
.then((res) => setData(res.data[0]));
}, []);
return (
<View style={styles.container}>
<Text>Welcome {data.name}!</Text>
<StatusBar style="auto" />
</View>
);
Then i will get this.
[AxiosError: Network Error]
CodePudding user response:
Your Express app needs to use CORS (Cross-Origin Resource Sharing). Add the following to your server file:
var app = express();
// ADD THIS
var cors = require('cors');
app.use(cors());
CodePudding user response:
had similiar issue before with React-Native
when running on Android Emulator
solution for me was changing the api url from http://localhost:5000/
to http://10.0.2.2:5000/