Fetch data from local Json server in React-Native

My Local Json Server data is something like this: Json

I used it in server:

import axios from 'axios';
const Server = axios.create({
  baseURL: 'http://localhost:3000/office',
  timeout: 5000,
  headers: {'accept': 'application/json'}
});
export default Server;

But I'm unable to fetch it, I need help in fetching it from local json server.

export function fetchJobs(props) {
  return function(dispatch){
    dispatch({type: JobsTypes.FETCH_OFFICE});
    Server
    .get('/office')
    .then((response) => {
      dispatch({type: JobsTypes.FETCH_OFFICE_SUCCESSFUL,payload:response.data });
    })
    .catch((err) => {
      dispatch({type:JobsTypes.FETCH_OFFICE_FAILED,payload:err});
    });
  }
} 

1 answer

  • answered 2018-04-17 04:14 flaky

    I assume you're using Android Virtual Device?

    Localhost is going to be: 10.0.2.2 So in your case you have to use: 'http://10.0.2.2:3000'

    Let me know if this works for you.