0

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});
    });
  }
} 
6
  • I think you need to change baseURL: 'http://localhost:3000' Commented Apr 17, 2018 at 4:26
  • @PritishVaidya Tried it, still the same. Thank you. Commented Apr 17, 2018 at 4:52
  • What error do you get? Also try to log the error and response. Commented Apr 17, 2018 at 4:53
  • There is no error in it, the page is just blank and in network response there is no localhost 3000 Commented Apr 17, 2018 at 5:05
  • You are trying using debug mode right? Commented Apr 17, 2018 at 9:06

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I have connected to my mobile via USB debugging.Still the same issue. Thank you.
Changed localhost to my computer IP and it worked fine, thank you
-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.