简体   繁体   English

在React-Native中从本地Json服务器取数据

[英]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.但是我无法获取它,我需要帮助从本地 json 服务器获取它。

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? 我假设您正在使用Android虚拟设备?

Localhost is going to be: 10.0.2.2 So in your case you have to use: 'http://10.0.2.2:3000' Localhost将是: 10.0.2.2因此,在您的情况下,您必须使用: 'http://10.0.2.2:3000' : 10.0.2.2 : 10.0.2.2 '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> 如果您在开发机上使用Android Virtual Device ,那么您需要使用http://10.0.2.2:<port>以便与在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. 如果您使用的是物理设备,请确保其与开发机器位于同一网络中,并用开发机器ip替换10.0.2.2部分。

I beleive you need to change your URL: 'http://localhost:3000'我相信你需要改变你的 URL: 'http://localhost:3000'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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