简体   繁体   中英

Fetch data from local Json server in React-Native

My Local Json Server data is something like this: 杰森

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});
    });
  }
} 

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.

Ok, just to clarify for anyone else who gets to this answer with the same question:

If you're using Android Virtual Device on your dev machine THEN you need to use http://10.0.2.2:<port> in order to communicate with your dev server running on http://localhost:<port>

If you're using a physical device, make sure it is in the same network as your dev machine and replace the 10.0.2.2 part with your dev machines ip.

I beleive you need to change your URL: 'http://localhost:3000'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM